question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Return Promise if callback is not provided?

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:27
  • Comments:8

github_iconTop GitHub Comments

12reactions
aearlycommented, 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.

2reactions
ex1stcommented, 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);
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to make a function return a promise if optional callback is ...
You can do it like so: function squareAsync(val, callback) { const timeout = function(res, rej){ setTimeout(function(){ if (Math.random() ...
Read more >
How to make a Promise out of a Callback function in JavaScript
Solution 2 (involved): Turn the Callback into a Promise. Sometimes, using the request and util libraries is just not possible, whether it's ...
Read more >
Using promises - JavaScript - MDN Web Docs
Essentially, a promise is a returned object to which you attach callbacks, instead of passing callbacks into a function.
Read more >
Converting Callbacks to Promises in Node.js - Stack Abuse
Let's talk about how to covert callbacks to promises if the util.promisify() function is not available. The idea is to create a new...
Read more >
25. Promises for asynchronous programming - Exploring JS
Promises provide a better way of working with callbacks: Now an asynchronous function returns a Promise, an object that serves as a placeholder...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found