1

I am using Blubird and Sequelize (which uses Blubird under the covers).

Suppose I have a code similar to:

Feed.findAll()
    .map(function (feed) { //  <---- this is what I'm interested in below
        // do some stuff here
        return some_promise_here;
    })
    .map(function (whatever) {
        // What is the best way to access feed here?
    })
    ....

I have found some replies which hinted at possible solutions, but I can't quite put my finger on it.

I have tried with Promise.all(), .spread(), but I never managed to make it work.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
cgf
  • 3,369
  • 7
  • 45
  • 65

2 Answers2

2
Feed.findAll()
    .map(function (feed) { //  <---- this is what I'm interested in below
        // do some stuff here
        return some_promise_here.then(function(result){
            return { result: result, feed: feed};// return everything you need for the next promise map below.
        });
    })
    .map(function (whatever) {
        // here you are dealing with the mapped results from the previous .map
        // whatever -> {result: [Object],feed:[Object]}
    })
bluetoft
  • 5,373
  • 2
  • 23
  • 26
2

This looks very similar to How do I access previous promise results in a .then() chain?, however you're dealing with a .map call here and seem to want to access the previous result for the same index of the processed array. In that case, not all solutions do apply, and a closure seems to be the simplest solution:

Feed.findAll().map(function (feed) { 
    // do some stuff here
    return some_promise_here.then(function (whatever) {
        // access `feed` here
    });
})

You can apply explicit pass-through as well, though, like outlined in @bluetoft's answer.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I used a closure before, but I had a giant nested chain of promises and that's why I went with @bluetoft's answer. Thanks, yours certainly works as well! – cgf Jun 30 '15 at 23:35