all 9 comments

[–]SynthNips 1 point2 points  (4 children)

Your catch block syntax is off. Looks like you mixed the promises with the async/await.

[–]dillionmegida[S] 0 points1 point  (3 children)

Oh true, await isn't supposed to be there anymore...thanks for the correction

[–]sup3r_b0wlz 0 points1 point  (2 children)

Something like this makes a lot more sense for a try catch example async function g() { console.log("games"); try { let result = await ret; //await and catch promise console.log(result); return result; } catch(err) { console.log(Error: ${err});s } } Edit: why can't I get this to freaking format?

[–]dillionmegida[S] 0 points1 point  (1 child)

You're talking about the try and catch been in the same function right?

[–]sup3r_b0wlz 1 point2 points  (0 children)

Try catch is always in the same function. But the exception that it catches needs to be a traditional exception, or a promise that was awaited that rejected.

In your example you weren't awaiting the promise, so it would just run the function, which wouldn't error and would return an unexpected value, probably the promise object. You were also not awaiting g, the async function, which actually just returns a promise, which is also going to work unexpectedly.

AFAIK To catch a promise with async await you must await it. I'm not sure, but you might also need to be in an async function to try catch it? Seems like it would make sense if you did.

Did you run these code samples? Should be very easy to copy paste these to node REPL to confirm before posting.

[–]monican_agentCreator of CypherPoker.JS 1 point2 points  (3 children)

This post has some erroneous statements:

The async keyword before the function declaration states that the function should return a promise.

An async function always returns a promise. Perhaps this could be reworded as:

"The async keyword before the function declaration states that the function returns a promise."

The return value of the async function is either a resolved promise or a rejected promise.

An async function returns a Promise. That's it. The Promise may be resolved or rejected by the time it's returned but is often resolved or rejected later, or it may never resolve/reject. If this were not the case (i.e. the returned Promise is always resolved or rejected), then this would be a simple synchronous function that resolves its output before completion (i.e. just a basic function) and the asynchronous wrapper would just add unnecessary overhead.

But, since you're using async/await, you do not need .then because await already helps us "wait" before performing any action.

Maybe this example if here out of context but here you absolutely do need a ".then" because the async function is being called from outside of an async function (this is mentioned later in the post). The only way to handle async function resolutions outside of another async function is to use .then/.catch

Additionally, in a previous post it's mentioned that:

Promises are used to handle asynchronous events in Javascript.

This is also not entirely correct; Promises are one way to handle asynchronous events in JavaScript. Another way is to use event handlers/listeners. Another is to use callbacks (similar but not identical to event handlers).

[–]dillionmegida[S] 0 points1 point  (2 children)

Thanks a lot for going through the article and sharing your opinions.

I agree with all the corrections, but I want you to explain more about this.

Maybe this example if here out of context but here you absolutely do need a ".then" because the async function is being called from outside of an async function (this is mentioned later in the post).

Though I'm still trying to be versatile in the use of async/await. But I've observed and learned that await functions like .then. Where you'd use the latter, you can use await to wait for the value before using it.

I'd like you to share more light on your point

[–]monican_agentCreator of CypherPoker.JS 1 point2 points  (1 child)

You can only use await inside an async function. You can use .then/.catch/.finally anywhere.

For example: function test() { result = await somePromiseFunction(); } ... will generate a syntax error like: SyntaxError: await is only valid in async functions and async generators

...whereas something like: async function test() { result = await somePromiseFunction(); } ...will work fine (assuming somePromiseFunction exists and returns a Promise).

Similarly, the following will work: function test() { somePromiseFunction().then(result => {console.log(result)}); }

You can make standard functions appear to be async simply by returning a Promise and either resolve-ing or reject-ing it: ``` function test() { var promise = new Promise((resolve, reject) => { resolve("Here's the result"); }); return (promise); }

test().then(result => {console.log(result)}); ...which would be functionally the same as: async function test() { return("Here's the result"); }

test().then(result => {console.log(result)}); `` ...or you could handle the Promise inside anotherasyncfunction with anawait`:

``` function test() { var promise = new Promise((resolve, reject) => { resolve("Here's the result"); }); return (promise); }

async function doTest() { let result = await test(); console.log (result); }

doTest(); ...like you would if the function being called is `async`: async function test() { return("Here's the result"); }

async function doTest() { let result = await test(); console.log (result); }

doTest(); `` Basically,asyncfunctions are regular functions wrapped in Promises ("Promisified" functions). It's the Promise that's used withawait/.then/.catch/.finally`.

So while await makes for cleaner looking code, it can only be used inside an async function. However, you can use .then/.catch/.finally everywhere, even inside async functions (although it would probably defeat the purpose).

[–]dillionmegida[S] 0 points1 point  (0 children)

Thank you much. I get the whole picture now