0

I'm using co to execute a generator with a bunch of http requests:

co(function *(){
  // resolve multiple promises in parallel 
  var a = httpRequest(...);
  var b = httpRequest(...);
  var c = httpRequest(...);
  var res = yield [a, b, c];
  console.log(res);
}).catch(onerror);

Is there a way for me to introduce a second of sleep between each http request? Thanks.

1 Answers1

0

Yes, you can. Each yield - return new promise with dilay (in promise - you have to fire resolve or reject acording to httpRequest callback)
Try in this way

co(function *(){
var a = yield new Promise(function(resolve, reject){
    setTimeout(function () {
        //rosolve or reject this promise in callback of httpRequest();
    }, 1000)
});
var b = yield new Promise(function(resolve, reject){
    setTimeout(function () {
        //rosolve or reject this promise in callback of httpRequest();
    }, 1000)
});
var c = yield new Promise(function(resolve, reject){
    setTimeout(function () {
        //rosolve or reject this promise in callback of httpRequest();
    }, 1000)
});
var res = [a, b, c];
    console.log(res); 
}).catch(onerror);
Denis Lisitskiy
  • 1,285
  • 11
  • 15