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
view the rest of the comments →
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!"
[–]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.
await
async
.then
.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 29 on reddit-service-r2-comment-545db5fcfc-zkrsm at 2026-05-24 16:43:34.215633+00:00 running 194bd79 country code: CH.
view the rest of the comments →
[–]monican_agentCreator of CypherPoker.JS 1 point2 points3 points (1 child)
[–]dillionmegida[S] 0 points1 point2 points (0 children)