I am attempting to use promise to wait for async.mapLimit. I use it to run several shell scripts at the same time and I want to wait for all of them to finish executing before I continue past the log 'the end'. But I always get a undefined when using the return value for promise.
var myArray = [5,1,2,3,4];
const async = require('async');
const exec = require('child_process').exec;
function understandPromise() {
const _waitForMe = async.mapLimit(myArray, 16, doSomeThing, function(err, results){
console.log(results.length, 'should equal (doSomeThing)', myArray.length);
console.log('err',err);
});
// This also gives undefined
//const _waitForMe = async.mapLimit(myArray, 16, doSomeThing).then(a,b);
_waitForMe.then(a,b);
console.log('the end');
}
function doSomeThing(item, callback){
let runCmd = './test.sh ' + item;
console.log('before', runCmd);
exec(runCmd, function (error, stdout, stderr) {
console.log('error', error);
console.log('stderr', stderr);
console.log('stdout', stdout);
console.log('after', runCmd);
callback(null, item); // or actually do something to item
});
}
understandPromise();
TypeError: Cannot read property 'then' of undefined relating to _waitForMe
Why doesn't mapLimit return a promise? I realize I am doing something fundamentally wrong here but i cannot figure out what. Other solutions not involving promise is also something I could consider.
Skipping the callback yields the same problem 'then' of undefined
const _waitForMe = async.mapLimit(myArray, 16, doSomeThing);
Similar SO questions like this only gives errors async.mapLimit with Promise
Update 1 after comments Commenters have suggested this:
async function understandPromise() {
let results = await async.mapLimit(myArray, 8, doSomeThing);
console.log('results', results.length);
console.log('the end');
}
But this leads to (node:567) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of undefined.
I still can't understand why the return of async.mapLimit is undefined when there is no callback involved.
Update 2
I got this to work from the comments but in the end it was that the async module was at an old version 1.5.2 and not at the newer 3.2.0