Await an Async Function and Do Something with it in One Line of Javascript
August 23, 2020
Have you ever wanted a one-liner that let’s you await
the result of a function and do some further processing?
Instead of:
const resp = await fetch('https://api.github.com/users/johnomarkid')
const body = await json()
You want a way to skip declaring a resp
constant and get directly to the body.
Here’s some syntactical sugar in Javascript that let’s you do just that.
const body = (await fetch('https://api.github.com/users/johnomarkid')).json()