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

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Kosmetika
  • 20,774
  • 37
  • 108
  • 172
  • It also works with thenables :-) – Bergi Jul 08 '15 at 14:02
  • @Bergi are thenables a superset of promises? I'd never considered them as separate entities. – Evan Davis Jul 08 '15 at 14:04
  • @Bergi which are promises :) – Kosmetika Jul 08 '15 at 14:04
  • 1
    @Mathletics: Yes, [exactly](https://promisesaplus.com/). Thenables are objects with a `then` method of unknown functionality and origin. – Bergi Jul 08 '15 at 14:05
  • 1
    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 `then`able means you can assimilate promises from different libraries in `await`. – Benjamin Gruenbaum Jul 08 '15 at 14:06
  • @Bergi cool, thanks! – Evan Davis Jul 08 '15 at 14:16
  • @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? – hippietrail Jun 22 '16 at 02:29
  • @hippietrail All promises are thenables, but not all thenables are promises. – callum Aug 01 '16 at 14:32

2 Answers2

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.

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • Yeah, I agree that it's easy to switch, my confusion was because of see - http://stackoverflow.com/questions/31294521/es7-async-await-functions-do-they-work-only-with-promises/31294633#comment50580127_31294633 – Kosmetika Jul 08 '15 at 13:53
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

Evan Davis
  • 35,493
  • 6
  • 50
  • 57
  • I just noticed that if you're using request https://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? – Kosmetika Jul 08 '15 at 13:51
  • @Kosmetika you can not await the request library directly, but you can easily `promisifyAll` it and use it with promises with very low performance overhead. – Benjamin Gruenbaum Jul 08 '15 at 13:53
  • @BenjaminGruenbaum but it awaits without promisifying that caused my confusion! – Kosmetika Jul 08 '15 at 13:54
  • @Kosmetika no, it really doesn't - http://babeljs.io/repl/#?experimental=true&evaluate=true&loose=false&spec=false&playground=false&code=function%20fn(cb)%7B%0A%20%20setTimeout(()%3D%3E%20cb(null%2C%203)%2C%201000)%3B%0A%7D%0Aasync%20function%20foo()%7B%0A%20%20let%20val%20%3D%20await%20fn()%3B%0A%20%20console.log(typeof%20val%2C%20val)%3B%0A%20%20%0A%7D%0Afoo()%3B what happened is that `request` was probably promisified before by a library that wraps APIs for you like bluebird. – Benjamin Gruenbaum Jul 08 '15 at 13:55
  • @BenjaminGruenbaum strange days strange ways.. :) – Kosmetika Jul 08 '15 at 14:00