Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return Promise if callback is not provided? #1515

Closed
rkt2spc opened this issue Feb 27, 2018 · 8 comments
Closed

Return Promise if callback is not provided? #1515

rkt2spc opened this issue Feb 27, 2018 · 8 comments

Comments

@rkt2spc
Copy link

rkt2spc commented Feb 27, 2018

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);
}
@hargasinski
Copy link
Collaborator

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?

@aearly
Copy link
Collaborator

aearly commented Mar 2, 2018

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.

@ralphdas
Copy link

ralphdas commented Mar 6, 2018

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.

@rorymadden
Copy link

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

@aearly
Copy link
Collaborator

aearly commented Apr 15, 2018

Finally dug out my old experiment: #1526

@rodrigogs
Copy link

+1

@tritoanst
Copy link

tritoanst commented Jul 26, 2018

  • 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);
}

@ex1st
Copy link

ex1st commented Jul 26, 2018

@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);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants