How would you implement async/await functionality if you had to? - We’re talking about normal JS syntax (ES6) that does something like async/await, of course, not adding it to the syntax)
Asked
Active
Viewed 377 times
-1
-
What? using *async function(){ await someotherasync();}* ... – Jonas Wilms May 11 '17 at 17:19
-
Please be more specific – MeestorHok May 11 '17 at 17:19
-
You're question is not really clear. Are you looking for Promise functionality? – Peter LaBanca May 11 '17 at 17:22
-
I use generator functions that yield promises. Something like [bluebird's `Promise.coroutine`](http://bluebirdjs.com/docs/api/promise.coroutine.html) can then be used to wrap those functions into a single `Promise`-returning function. You end up with something syntactically almost identical to `async`/`await`. My code still uses this approach since `async` is not (quite) standardized yet. – Stephen Cleary May 11 '17 at 20:04
1 Answers
1
async
/await
are syntactic sugar on top of ES6 Promises.
For example, a program that uses async/await like this
async function asyncExample () {
const foo = await doStuff();
return foo + 1;
}
is equivalent to the following program in ES6
function promiseExample () {
return doStuff().then(foo => foo + 1);
}
Functions marked async
just return a Promise that resolves to the return value of the function.

Ari Lotter
- 605
- 8
- 23