Reputation: 21304
I started using async/await
ES7 functions in my js applications (transpiled by Babel).
Correct me if wrong, but do they work only with Promises? If yes, this means that I need to wrap regular callback functions into Promises (what I'm currently doing btw).
Upvotes: 13
Views: 2755
Reputation: 276286
The current (and likely final) async/await proposal awaits promises and desugars into something like bluebird's Promise.coroutine
with await
playing the part of yield
.
This makes sense, as promises represent a value + time and you're waiting for that value to become available. Note await
also waits for promise like constructs in all other languages that include it like C# or Python (3.5+) .
Note that converting callback APIs to promises is very easy, and some libraries offer tools to do so in a single command. See How to convert an existing callback API to promises for more details.
Upvotes: 10
Reputation: 36592
Yes, you await
a promise.
async function myFunction() {
let result = await somethingThatReturnsAPromise();
console.log(result); // cool, we have a result
}
http://pouchdb.com/2015/03/05/taming-the-async-beast-with-es7.html
Upvotes: 2