Published at
Updated at
Reading time
1min
This post is part of my Today I learned series in which I share all my web development learnings.

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.

If you enjoyed this article...

Join 5.5k readers and learn something new every week with Web Weekly.

Web Weekly — Your friendly Web Dev newsletter
Reply to this post and share your thoughts via good old email.
Stefan standing in the park in front of a green background

About Stefan Judis

Frontend nerd with over ten years of experience, freelance dev, "Today I Learned" blogger, conference speaker, and Open Source maintainer.

Related Topics

Related Articles