Await in async functions works for any thenable (#tilPost)

async/await makes it possible to write asynchronous JavaScript that looks synchronous. It helps to fight the "callback hell". But what statements can we actually use in combination with await?
Šime Vidas and Axel Rauschmay…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

async/await makes it possible to write asynchronous JavaScript that looks synchronous. It helps to fight the "callback hell". But what statements can we actually use in combination with await?

Šime Vidas and Axel Rauschmayer had a really interesting Twitter conversation recently. So let's look at some snippets.

(async () => { console.log(await 'foo'); })(); // 'foo'
(async () => { console.log(await 5); })();     // 5

It turns out that you can really await anything. I didn't know that. This is one of these tiny JS details I really like to discover. If you await something that's not a promise it will return the actual value.

let thenable = {
  then: (fn) => {
    fn('jup')
  }
};

(async () => { console.log(await thenable); })() // 'jup'

And... it doesn't have to be a promise. A thenable (anything that includes a function called then) works also fine. So thanks Šime and Axel for having these conversations in public.


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2017-09-04T22:00:00+00:00) Await in async functions works for any thenable (#tilPost). Retrieved from https://www.scien.cx/2017/09/04/await-in-async-functions-works-for-any-thenable-tilpost/

MLA
" » Await in async functions works for any thenable (#tilPost)." Stefan Judis | Sciencx - Monday September 4, 2017, https://www.scien.cx/2017/09/04/await-in-async-functions-works-for-any-thenable-tilpost/
HARVARD
Stefan Judis | Sciencx Monday September 4, 2017 » Await in async functions works for any thenable (#tilPost)., viewed ,<https://www.scien.cx/2017/09/04/await-in-async-functions-works-for-any-thenable-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » Await in async functions works for any thenable (#tilPost). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2017/09/04/await-in-async-functions-works-for-any-thenable-tilpost/
CHICAGO
" » Await in async functions works for any thenable (#tilPost)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2017/09/04/await-in-async-functions-works-for-any-thenable-tilpost/
IEEE
" » Await in async functions works for any thenable (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2017/09/04/await-in-async-functions-works-for-any-thenable-tilpost/. [Accessed: ]
rf:citation
» Await in async functions works for any thenable (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2017/09/04/await-in-async-functions-works-for-any-thenable-tilpost/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.