Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?)

  • R
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
139 views
Welcome To Ask or Share your Answers For Others

1 Answer

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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...