can I use async.waterfall inside async.parallel? - javascript

I want to call two functions and get the results in parallel but one of the function's results needed to be adapted. So the function structure is:
function test(cb) {
async.parallel({
func1: function foo(cb1) {
cb(null, {});
},
func2: function bar(cb2) {
async.waterfall([
function adapt1(next) {
//do something;
},
function adapt2(next) {
//do something;
}
], function handler(err, res) {
//do something.
})
}
}, function handler2(err, res) {
cb(null, {});
})
}
However, it just seems hang there forever. not sure if I can use async in this way....

Sure you can! You have to be sure to call your callbacks in the correct order and in the first place. For example, func1 should be calling cb1 not cb. Secondly, your waterfall is not invoking their callbacks at all.
Take this code for example.
'use strict';
let async = require('async');
function test(callback) {
async.parallel({
func1: function(cb) {
cb(null, { foo: 'bar' });
},
func2: function(cb) {
async.waterfall([
function(cb2) {
cb2(null, 'a');
},
function(prev, cb2) {
cb2(null, 'b');
}
], function(err, result) {
cb(err, result);
});
}
}, function(err, results) {
callback(err, results);
});
}
test(function(err, result) {
console.log('callback:', err, result);
});
Outputs: callback: null { func1: { foo: 'bar' }, func2: 'b' }

Related

Async.forEach call the callback

How to call the foreach callback after complete the series?
My code:
async.forEach(rowsIni, function (row, callback2) {
strQuery = "SELECT ...";
DB.query(strQuery, row, function (err, rows) {
if (err) {
console.log("SQL Error(SELECT) > " + err.message);
} else {
async.series([
function (callback) {
async.parallel([
function (callback) {
callback();
},
function (callback) {
callback();
}
], function (err) {
callback();
});
},
function (callback) {
callback();
}
], function() {
callback2
});
}
});
}, function (err) {
if (err) {
console.log("ASYNC Error > " + err.message);
}
console.log('END OF FOREACH');
callback(otherFunction);
});
How to call the callback2? If I call thus, the callback2 is not called.
async.series([...], function() {
callback2 // does nothing
})
You don't actually call callback2. Either use callback2() (call it) or
async.series([...], callback2);
To just pass the callback function.
edit:
async.eachSeries(rowsIni, function (row, callback2) {
...
async.series([
function (callback) {
async.parallel([
... functions
], callback);
},
function (callback) {
callback();
}
], callback2);
});

async.js : async.eachSeries result always throw undefined

Is there something wrong with my code?, I use async.eachSeries but my result always throw undefined.
Here my code :
async.eachSeries([1,2,3], function(data, cb) {
setTimeout(function() {
cb(null, data+1);
}, 1000);
}, function(err, result) {
console.log(err, result);
});
My log returned : null, undefined instead of null, [2,3,4]
thanks... and sorry for my terrible english XD
The second argument is called when the iteration is done and with eachSeries(), it takes only one parameter, err. If you want result, you have to use mapSeries:
async.mapSeries([1, 2, 3],
function (data, cb) {
setTimeout(function () {
cb(null, data + 1);
}, 1000);
},
function (err, result) {
console.log(result);
}
);
You can use a form of result with eachSeries() as well:
var result = [];
async.eachSeries([1,2,3], function(data, cb) {
setTimeout(function() {
result.push(data+1);
cb(null);
}, 1000);
}, function(err) {
console.log(err, result);
});
That should work, although I'm not able to test it myself at the moment.

Async series's callback doesn't fire

My problem here is that when I use this code I always get the callback in ensureAppIsValid and the one in the Async series seems to be never fired
var ReviewProcess = function (args) {
'use strict';
assert(args.application, 'Need an application to review');
this.app = args.application;
};
ReviewProcess.prototype.ensureAppIsValid = function (callback) {
'use strict';
if (this.app.isValid()) {
callback(null, this.app);
} else {
callback(this.app.validationMessage(), null);
}
};
ReviewProcess.prototype.processApplication = function (callback) {
'use strict';
async.series([
this.ensureAppIsValid(callback)
], function (err, callback) {
if (err) {
return callback(null, {
success: false,
message: err
});
}
callback(null, {
success: true,
message: 'Welcome to Mars'
});
});
};
It looks like you're using the word 'callback' too many times and the code isn't doing what you're expecting it to. You are passing the top level callback into the ensureAppIsValid() function, so once that function executes it doesn't go to async's callback. It also looks like you don't need the extra callback in async's follow up.
How about this:
ReviewProcess.prototype.processApplication = function (callback) {
'use strict';
async.series([
this.ensureAppIsValid(cb)
], function (err) {
if (err) {
return callback(null, {
success: false,
message: err
});
}
callback(null, {
success: true,
message: 'Welcome to Mars'
});
});
};
Async series requires a lists of functions as tasks to be ran.
You pass
this.ensureAppIsValid(callback)
But this is the call of the function, not the function itself.
Try this:
Async.series([
this.ensureAppIsValid.bind.apply(this.ensureAppIsValid, [null, [callback]])
], ... )
You shouldn't pass argument callback to this.ensureAppIsValid(). Instead, use here local callback parameter. For example, named cb.
Try:
var ReviewProcess = function (args) {
'use strict';
assert(args.application, 'Need an application to review');
this.app = args.application;
};
ReviewProcess.prototype.ensureAppIsValid = function (callback) {
'use strict';
if (this.app.isValid()) {
callback(null, this.app);
} else {
callback(this.app.validationMessage(), null);
}
};
ReviewProcess.prototype.processApplication = function (callback) {
'use strict';
async.series([
this.ensureAppIsValid(cb)
], function (err, callback) {
callback(null, {
success: !err,
message: err? err : 'Welcome to Mars'
});
}
});
};
I have also slightly changed your eventual callback in async.series. Now it's more compact.

How to delay all the calls to any method of a javascript object until the object is initialized?

Suppose this simple piece of code:
/*jslint node: true */
"use strict";
function MyClass(db) {
var self = this;
this._initError = new Error("MyClass not initialized");
db.loadDataAsyncronously(function(err, data) {
if (err) {
self._initError =err;
} else {
self._initError = null;
self.data = data;
}
});
}
MyClass.prototype.getA = function(cb) {
if (this._initError) {
return cb(this._initError);
}
return cb(null, this.data.a);
};
MyClass.prototype.getB = function(cb) {
if (this._initError) {
return cb(this._initError);
}
return cb(null, this.data.b);
};
var db = {
loadDataAsyncronously: function(cb) {
// Emulate the load process from disk.
// Data will be available later
setTimeout(function() {
cb(null, {
a:"Hello",
b:"World"
});
},1000);
}
};
var obj = new MyClass(db);
obj.getA(function (err, a) {
if (err) {
console.log(err);
} else {
console.log("a: " + a);
}
});
obj.getB(function (err, b) {
if (err) {
console.log(err);
} else {
console.log("a: " + b);
}
});
This gives an error because obj is not initialized when getA and getB methods are called. I would like that any method called before the object is initialized, be delayed automatically until the class finish its initialization.
One way to solve it is this way:
/*jslint node: true */
"use strict";
function MyClass(db) {
var self = this;
self._pendingAfterInitCalls = [];
db.loadDataAsyncronously(function(err, data) {
if (!err) {
self.data = data;
}
self._finishInitialization(err);
});
}
MyClass.prototype.getA = function(cb) {
this._waitUntiliInitialized(function(err) {
if (err) {
return cb(err);
}
return cb(null, this.data.a);
});
};
MyClass.prototype.getB = function(cb) {
this._waitUntiliInitialized(function(err) {
if (err) {
return cb(err);
}
return cb(null, this.data.b);
});
};
MyClass.prototype._finishInitialization = function(err) {
this._initialized=true;
if (err) {
this._initError = err;
}
this._pendingAfterInitCalls.forEach(function(call) {
call(err);
});
delete this._pendingAfterInitCalls;
};
MyClass.prototype._waitUntiliInitialized = function(cb) {
var bindedCall = cb.bind(this);
if (this._initialized) {
return bindedCall(this._initError);
}
this._pendingAfterInitCalls.push(bindedCall);
};
var db = {
loadDataAsyncronously: function(cb) {
// Emulate the load process from disk.
// Data will be available later
setTimeout(function() {
cb(null, {
a:"Hello",
b:"World"
});
},1000);
}
};
var obj = new MyClass(db);
obj.getA(function (err, a) {
if (err) {
console.log(err);
} else {
console.log("a: " + a);
}
});
obj.getB(function (err, b) {
if (err) {
console.log(err);
} else {
console.log("a: " + b);
}
});
But it seems to me a lot of overhead to be written for each class following this pattern.
Is there a more elegant way to handle this functionality?
Does exist any library to simplify this functionality?
Preparing this question, came to my head what probable would be a good answer. The idea is to use the concept of a factory function. The code above would be rewritten this way.
/*jslint node: true */
"use strict";
function createMyClass(db, cb) {
var obj = new MyClass();
obj._init(db, function(err) {
if (err) return cb(err);
cb(null, obj);
});
}
function MyClass() {
}
MyClass.prototype._init = function(db, cb) {
var self=this;
db.loadDataAsyncronously(function(err, data) {
if (err) return cb(err);
self.data = data;
cb();
});
};
MyClass.prototype.getA = function(cb) {
if (this._initError) return cb(this._initError);
cb(null, this.data.a);
};
MyClass.prototype.getB = function(cb) {
if (this._initError) return cb(this._initError);
cb(null, this.data.b);
};
var db = {
loadDataAsyncronously: function(cb) {
// Emulate the load process from disk.
// Data will be available later
setTimeout(function() {
cb(null, {
a:"Hello",
b:"World"
});
},1000);
}
};
var obj;
createMyClass(db,function(err, aObj) {
if (err) {
console.log(err);
return;
}
obj=aObj;
obj.getA(function (err, a) {
if (err) {
console.log(err);
} else {
console.log("a: " + a);
}
});
obj.getB(function (err, b) {
if (err) {
console.log(err);
} else {
console.log("a: " + b);
}
});
});
I share this Q/A because I thing it can be interesting to some body else. If you thing there exist better solutions, libraries to handle this situations, or any other idea, I will appreciate it.
The usual strategy here is to NOT use any async operation from a constructor. If an object needs an async operation in order to initialize it, then you use one of two options:
The async portion of the initialization is done in an .init(cb) method that must be called by the creator of the object.
You use a factory function that takes a callback that is called when the async portion of the operation has completed (like your proposed answer).
If you create a lot of these, perhaps the factory function makes sense because it saves you a little repeated code. If you don't create a lot of them, I prefer the first option because I think it makes it a lot clearer in the code exactly what is happen (you create an object and then you initialize it asynchronously and then the code continues only when the async operation has completed).
For the first option, it could look like this:
function MyClass(...) {
// initialize instance variables here
}
MyClass.prototype.init = function(db, callback) {
var self = this;
db.loadDataAsyncronously(function(err, data) {
if (err) {
self._initError = err;
} else {
self._initError = null;
self.data = data;
}
callback(err);
});
}
// usage
var obj = new MyClass(...);
obj.init(db, function(err) {
if (!err) {
// continue with rest of the code that uses this object
// in here
} else {
// deal with initialization error here
}
});
Personally, I would probably use a design using promises so the calling code could look like this:
var obj = new MyClass(...);
obj.init(db).then(function() {
// object successfully initialized, use it here
}, function(err) {
// deal with initialization error here
});
You can use a Promise internally in MyClass and have all functions wait for the promise to complete before proceeding.
http://api.jquery.com/promise/
You could inject the precondition into the functions:
function injectInitWait(fn) {
return function() {
var args = Array.prototype.slice.call(arguments, 0);
this._waitUntiliInitialized(function(err) {
if (err) return args[args.length - 1](err);
fn.apply(this, args);
});
}
}
injectInitWait(MyClass.prototype.getA);
injectInitWait(MyClass.prototype.getB);
That being said, your factory approach would be superior.

Retry an async function if the error is retryable

How can I change my logic to retry if the err.retryable = true in the following code:
async.each(queues, function (queue, callback) {
sqs.getQueueUrl({'QueueName': queue.queue}, function (err, qurl) {
if (err) {
if (err.retryable) {
// How to retry sqs.getQueueUrl({'QueueName': queue.queue}...?
} else {
console.error(err, err.stack);
callback(err);
}
}else{
//Do lots of things here
}
})
}, function (err) {
//...
})
In addition to the advice by dfsq to name your callback and use it an asynchronously recursive manner, see also async.retry from the async module by Caolan McMahon. Example:
async.retry(3, apiMethod, function(err, result) {
// do something with the result
});
More complex example:
async.auto(
{
users: api.getUsers.bind(api),
payments: async.retry(3, api.getPayments.bind(api))
}, function(err, results) {
// do something with the results
}
);
More details in the docs.
UPDATE
A better solution for your use case:
I wrote a utility function that you can use to make your original method support any number of retries (with err.retryable support).
You can use it this way:
var retryingFunction = withRetries(sqs, sqs.getQueueUrl);
(Note that you need to provide both sqs and sqs.getQueueUrl)
And now you can use the retryingFunction just like you would use sqs.getQueueUrl but with a number of retries as the first arguments. The retries will only be done when err.retryable is true.
So now, instead of:
sqs.getQueueUrl({'QueueName': queue.queue}, function (err, qurl) {
// ...
});
you can use:
retryingFunction(3, {'QueueName': queue.queue}, function (err, qurl) {
// ...
});
where 3 is the number of retries.
And this is the function that I wrote to make the above possible:
function withRetries(obj, method) {
if (!method) {
method = obj;
obj = null;
}
if (typeof method != "function") {
throw "Bad arguments to function withRetries";
}
var retFunc = function() {
var args = Array.prototype.slice.call(arguments);
var retries = args.shift();
var callback = args.pop();
if (typeof retries != "number" || typeof callback != "function") {
throw "Bad arguments to function returned by withRetries";
}
var retryCallback = function (err, result) {
if (err && err.retryable && retries > 0) {
retries--;
method.apply(obj, args);
} else {
callback(err, result);
}
};
args.push(retryCallback);
method.apply(obj, args);
};
return retFunc;
}
See this LIVE DEMO to play with it and see how it works.
It works fine in the demo, I hope it will also work for your code.
You can give queue callback a name and provide it in retry request again. Try this:
async.each(queues, function (queue, callback) {
sqs.getQueueUrl({'QueueName': queue.queue}, function queueCallback(err, qurl) {
if (err) {
if (err.retryable) {
sqs.getQueueUrl({'QueueName': queue.queue}, queueCallback);
} else {
console.error(err, err.stack);
callback(err);
}
} else {
//Do lots of things here
}
});
}, function (err) {
//...
});

Categories