ES2017 Async/await functions - do they work only with promises?
Asked Answered
P

2

13

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).

Postprandial answered 8/7, 2015 at 13:41 Comment(7)
It also works with thenables :-)Groupie
@Groupie are thenables a superset of promises? I'd never considered them as separate entities.Argos
@Groupie which are promises :)Postprandial
@Mathletics: Yes, exactly. Thenables are objects with a then method of unknown functionality and origin.Groupie
Yes, you can also wait for non-promise values, the spec literally says Promise.resolve will be called on the value so plain values will remain plain values and thenables will be converted to promises in a safe way. A thenable means you can assimilate promises from different libraries in await.Legality
@BenjaminGruenbaum: What does it mean to say "thenables will be converted to promises" when the preceding comments seem to clarify that thenables already are promises?Pudendum
@Pudendum All promises are thenables, but not all thenables are promises.Goodrow
L
10

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.

Legality answered 8/7, 2015 at 13:51 Comment(1)
Yeah, I agree that it's easy to switch, my confusion was because of see - #31295021Postprandial
A
2

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

Argos answered 8/7, 2015 at 13:45 Comment(5)
I just noticed that if you're using request github.com/request/request and doing await request.get('url') it will return response body. Does it mean that request lib methods not only callback based?Postprandial
@Postprandial you can not await the request library directly, but you can easily promisifyAll it and use it with promises with very low performance overhead.Legality
@BenjaminGruenbaum but it awaits without promisifying that caused my confusion!Postprandial
@Postprandial no, it really doesn't - babeljs.io/repl/… what happened is that request was probably promisified before by a library that wraps APIs for you like bluebird.Legality
@BenjaminGruenbaum strange days strange ways.. :)Postprandial

© 2022 - 2024 — McMap. All rights reserved.