0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Composing async functions

Posted at
const composeAsync = (...funcs) =>
  arg => funcs.reduceRight(async (acc, fn) => fn(await acc), arg);


async function double(v) {
  await new Promise(resolve => setTimeout(resolve, 1000));
  console.log(v);
  return v * 2;
}

composeAsync(double, double, double)(1)
  .then(console.log, console.error);
  • The reducer returns a Promise. So getting a value from the acc have to use the await keyword.
  • An initial value of the acc may be not a Promise. But it is not a problem, because the await keyword can accept a value other than a Promise.
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?