Asherlc Asherlc - 3 months ago 18
Javascript Question

Chain ES7 async functions

In an ES7 async function, I can get an asynchronous value like so:

const foo = await myAsyncFunction()


If I want to call a method on the result, with a sync function I'd do something like
myAsyncFunction().somethingElse()


Is it possible to chain calls with async functions, or do you have to assign a new variable for each result?

Answer

You can await in an expression, no need to assign it to a new variable.

const foo = await (await myAsyncFunction()).somethingElse()
Comments