Async wait for a function
I'm trying to send an SMS for a function. But the problem is: The function takes about 10-15 seconds to finish (Since we do bunch of stuff with PhantomJS).
_.each(users, function(userData){ // This does not work since i need to wait for 15 seconds
smsFree.sendSMSFree(userData, productUrl);
});
I've even tried using setTimeout but that didn't quite work as well.
I'm on NodeJS. How can I leverage Async or some other library to solve my problem?
I want to wait for 15 seconds then loop to the second object. Not sure how this is achieved. (Async.serial?)
You should use promise pattern with Q. Your function should return a promise and things will be easier:
Q.all(users.map(user => smsFree.sendSMSFree(userData, productUrl)))
.then(() => {
// Do stuff once every SMS has been successfully sent!
});
Or standard Promise
:
Promise.all(users.map(user => smsFree.sendSMSFree(userData, productUrl)))
.then(() => {
// Do stuff once every SMS has been successfully sent!
});
If your function doesn't use promise pattern you can either wrap it to use the whole pattern or you'll be stuck in terms of implement asynchronous continuations...
A quick and dirty solution that might work for you:
var n = 0;
_.each(users, function (userData) {
setTimeout(function () {
smsFree.sendSMSFree(userData, productUrl);
}, 15000 * n++;
});
It runs the functions for every iteration with 15000 milliseconds (15 seconds) intervals between them. This is the only way that you can do it unless sendSMSFree
either takes a callback or returns a promise.
If it returns a promise (which it might, you didn't explain how that function works in your question) and you want to run them all at the same time but wait until all of them have finished - see the answer by Matías Fidemraizer.
If you want to run them in series, ie run a new one as soon as the previous one finished, then it will be more complicated. If you want to add 15 second delay after the previous one finished before starting the next one, it will be more complicated still.
If the function takes a callback, then you can use async.series.
There's no point in explaining it in detail since we don't even know whether or not it returns a promise or takes callback at all - this is all speculation. You can see other answers that explain how promises and callbacks work if you're interested:
In summary:
If the function that you call in the loop neither returns a promise nor takes a callback - then you can only add timeouts to delay them.
If it returns a promise - you can use Q or Bluebird to control the flow.
If it takes a callback - you can use async to control the flow.
链接地址: http://www.djcxy.com/p/55386.html上一篇: 尝试/ catch异步/等待块
下一篇: 异步等待一个函数