Async: Return Promise if callback is not provided?

Created on 27 Feb 2018  ·  8Comments  ·  Source: caolan/async

Nice to have in ES2017 async/await development

const asyncjs = require('async');
const foo = async () => {
    const arr = Array.from({ length: 1000 }, (v, i) => i);
    const transformedArr = await asyncjs.mapLimit(arr, 100, async (v) => {
        return await Promise.resolve(v * 2);
    });
    console.log(transformedArr);
}
feature feedback-wanted

Most helpful comment

I've thought about this, and experimented with it a bit. People are going to want to await Async.mapLimit(arr, 10, async foo => {...}). It could very well be something we include in 3.0.

Also, some Async methods would be pretty silly with async/await, e.g. series, waterfall.

All 8 comments

Historically, async has been against this (see #1086).

However, with the new async/await syntax, node adding promise APIs in the future, and the support for promises having increased, I'd be open to revisiting this. Thoughts @aearly @megawac?

I've thought about this, and experimented with it a bit. People are going to want to await Async.mapLimit(arr, 10, async foo => {...}). It could very well be something we include in 3.0.

Also, some Async methods would be pretty silly with async/await, e.g. series, waterfall.

When using Async I sort of naturally assumed the return of a Promise. It was only at the dis-functioning of my code and my routine rtfm moment that I noticed the callback structure. The above would be very welcome since it would keep code as uniform ES7 as possible.

I agree that this would be useful.

In the interim, if anyone is stuck right now, I found this article good for explaining how to write a forEach loop with await.
https://codeburst.io/javascript-async-await-with-foreach-b6ba62bbf404

Finally dug out my old experiment: #1526

+1

  • Convert to async method
function makeAsync() {
  if (arguments.length < 3) return; // wrong params, not run or throw exception here!
  let fixParams = {};
  let paramLength = arguments.length;
  let method = arguments[0];
  for (let i = 1; i < arguments.length; i++) {
    fixParams[(i - 1)] = arguments[i];
  }
  return new Promise((resolve, reject) => {
    const callback = (err, result) => {
      if (err) return reject(err);
      return resolve(result);
    };

    fixParams[paramLength - 1] = callback;
    fixParams.length = paramLength;
    method.apply(null, fixParams);
  });
}

example1:

await makeAsync(async.each, openFiles, async (file) => {
  await asyncOperation(file);
});

example2:

const foo = async () => {
    const arr = Array.from({ length: 1000 }, (v, i) => i);
    const transformedArr = await makeAsync(async.mapLimit, arr, 100, async (v) => {
        return await Promise.resolve(v * 2);
    });
    console.log(transformedArr);
}

@tritoanst, your promisify function is overhead. It may be easier:

function promisify(original) {
    return function (...args) {
        return new Promise((resolve, reject) => {
            args.push((err, result) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(result);
                }
            });

            original.apply(this, args);
        });
    };
}

const foo = async () => {
    const arr = Array.from({ length: 1000 }, (v, i) => i);
    const transformedArr = await promisify(async.mapLimit)(arr, 100, async (v) => {
        return v * 2; // `return await` is redundant
    });
    console.log(transformedArr);
};
Was this page helpful?
0 / 5 - 0 ratings