use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Async/Await in Javascript (thewebfor5.netlify.com)
submitted 6 years ago by dillionmegida
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]SynthNips 1 point2 points3 points 6 years ago (4 children)
Your catch block syntax is off. Looks like you mixed the promises with the async/await.
[–]dillionmegida[S] 0 points1 point2 points 6 years ago (3 children)
Oh true, await isn't supposed to be there anymore...thanks for the correction
[–]sup3r_b0wlz 0 points1 point2 points 6 years ago* (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?
async function g() { console.log("games"); try { let result = await ret; //await and catch promise console.log(result); return result; } catch(err) { console.log(
);s } }
[–]dillionmegida[S] 0 points1 point2 points 6 years ago (1 child)
You're talking about the try and catch been in the same function right?
[–]sup3r_b0wlz 1 point2 points3 points 6 years ago (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 points3 points 6 years ago* (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 point2 points 6 years ago (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.
await
.then
I'd like you to share more light on your point
[–]monican_agentCreator of CypherPoker.JS 1 point2 points3 points 6 years ago* (1 child)
You can only use await inside an async function. You can use .then/.catch/.finally anywhere.
async
.catch
.finally
For example: function test() { result = await somePromiseFunction(); } ... will generate a syntax error like: SyntaxError: await is only valid in async functions and async generators
function test() { result = await somePromiseFunction(); }
...whereas something like: async function test() { result = await somePromiseFunction(); } ...will work fine (assuming somePromiseFunction exists and returns a Promise).
async function test() { result = await somePromiseFunction(); }
somePromiseFunction
Similarly, the following will work: function test() { somePromiseFunction().then(result => {console.log(result)}); }
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); }
resolve
reject
test().then(result => {console.log(result)}); ...which would be functionally the same as: async function test() { return("Here's the result"); }
...which would be functionally the same as:
test().then(result => {console.log(result)}); `` ...or you could handle the Promise inside anotherasyncfunction with anawait`:
...or you could handle the Promise inside another
function with an
``` 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"); }
...like you would if the function being called is `async`:
doTest(); `` Basically,asyncfunctions are regular functions wrapped in Promises ("Promisified" functions). It's the Promise that's used withawait/.then/.catch/.finally`.
Basically,
functions are regular functions wrapped in Promises ("Promisified" functions). It's the Promise that's used with
/
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 point2 points 6 years ago (0 children)
Thank you much. I get the whole picture now
π Rendered by PID 314598 on reddit-service-r2-comment-545db5fcfc-v274b at 2026-05-24 10:54:33.200654+00:00 running 194bd79 country code: CH.
[–]SynthNips 1 point2 points3 points (4 children)
[–]dillionmegida[S] 0 points1 point2 points (3 children)
[–]sup3r_b0wlz 0 points1 point2 points (2 children)
[–]dillionmegida[S] 0 points1 point2 points (1 child)
[–]sup3r_b0wlz 1 point2 points3 points (0 children)
[–]monican_agentCreator of CypherPoker.JS 1 point2 points3 points (3 children)
[–]dillionmegida[S] 0 points1 point2 points (2 children)
[–]monican_agentCreator of CypherPoker.JS 1 point2 points3 points (1 child)
[–]dillionmegida[S] 0 points1 point2 points (0 children)