简体   繁体   中英

sequential promise chain with array of data, how to resolve the promise chain?

I am using kriskowal q implementation.

I have an array of data objects, each with an id.

I need to chain these sequentially into promises because I am abiding to rate limiting rules by setting it to 1 request per second.

However, I am having trouble resolving the promises and my function stalls. Ie I see the ouput of addVideo, getInfo, retryIfNeeded and a delay of 1 second for the very first video, but I don't see any of that for any subsequent videos.

What I want to do is after the delay, to resolve that chain so that the next list of promises continues on the second Video ID.

How do I do this? What am I doing wrong? I've searched a lot on google but haven't found a solution so any suggestions is welcome

Edit added jsfiddle: http://jsfiddle.net/gpa7ym18/4

var promiseChain = data.items.reduce(function(promise, video) {
  video.type = type;
  return promise
         .then(addVideo)
         .then(getInfo)
         .then(retryIfNeeded)
         .then( function() {
            return q.delay(1000)
                   .done(function() {
                        NEED TO RESOLVE HERE but there is NO Defered object
                        to set defer.resolve. How do I resolve this promise chain?
                   });
         });
   }, q.resolve(data.items[0]));

You don't need to resolve anything.

You should simply return the delayed promise, and that will become the value of the entire chain.
This is exactly how promise chaining works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM