Terence Chow
Terence Chow

Reputation: 11153

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. I.e. 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]));

Upvotes: 0

Views: 578

Answers (1)

SLaks
SLaks

Reputation: 887415

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.

Upvotes: 1

Related Questions