0

My question involves promises and providing previous values of chain promises. The question is, is the item passed from first return promise to second promise "runItem --> testItem"?

Or do we have to pass the item through all promises?

Example:

db.items.find({_id: id}).then(function (res) {
  runItem(item);
})

function runItem(item) {
  removeFromQueue(item.id).then(function () {
    testItem(item);
  });
}

function testItem(item) {
  ...
}
  
function removeFromQueue(item) {
  return db.queue.remove({_id: item._id});
}

EDIT: Maybe this would be a better example:

Can we access original attribute item, or is it going to be overwritten when the next time function is called?

function start(id)
  db.find({_id: id}).then(function (item) {
    test(item).then(function (res) {
      // can we access original attribute item, or is it going to be overwritten when the next time function is called
      resolve({ res: res, item: item }); 
    });
  });
}

function test(item) {
  return $test(item).then(function () {
    resolve('success');  
  });
}
puppeteer701
  • 1,225
  • 3
  • 17
  • 33
  • You are supposed to return the promise objects. – thefourtheye Jul 11 '16 at 08:24
  • I don't understand what's you're trying to do with your code but no, the previous values aren't stored, you have to save them if you need them (in some cases it's convenient to use the ability of bluebird promises to be bound to an object). – Denys Séguret Jul 11 '16 at 08:24
  • I want to pass "item" to testItem function, but "item" is an attribute in runItem function. The code is general, mostly I just want to know, if it is possible, and if not what is the best way to pass variables from one promise to another. – puppeteer701 Jul 11 '16 at 08:29
  • Possible dupe: [How do I access previous promise results in a .then() chain?](http://stackoverflow.com/q/28250680/1426891) – Jeff Bowman Jul 11 '16 at 17:55
  • Maybe, but I would prefer a solution for my example, because most of the code in my project is organised like this. – puppeteer701 Jul 12 '16 at 12:26

3 Answers3

0

You can change the return value of a previous promise and simply return it. The next promise will be influenced. Check this example:

var promise = new Promise(function(res,rej){ res({data: 7}) }); // start with {data:7}
var promise1 = promise.then(function(res){ console.log('promise.then: ' + JSON.stringify(res)); return res.data; }); // log: {data:7}. return: 7
var promise2 = promise1.then(function(res) { console.log('promise1.then : ' + res); return res + 1;}); // log:7. return 8.
var promise3 = promise2.then(function(res) { console.log('promise2.then: ' + res); return res;}); // log:8. return 8.
var promise4 = promise3.then(function(res) { console.log('promise3.then: ' + res); return res;}); // log:8. return 8.
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
0

Try Promise.props function.

Something like

...
{
    ...
    return Promise.props({res: res, item: item}).
        .then(function(result){
            return Promise.resolve({ res: res, item: item }); 
        });
}
Anton Kandybo
  • 3,678
  • 4
  • 23
  • 31
-1

The example you have posted doesn't returns promise object .

Below is an example for the chaining things in promise, have a look.

functionA = function(text) {return new Promise(function(resolve) {
resolve(text);
console.log(text + " at function A");
});
}

functionB = function(text) {return new Promise(function(resolve) {
resolve(text);
console.log(text + " at function B");

});}


functionA("test").then(function (res) 
{
 console.log(res + " at then");
 runItem(res);

})

function runItem(item) {
 functionB(item).then(function (res) {
 console.log(res + " at Function Run Item");
testItem(res);

});
}

function testItem(item) {
console.log(item + " at Function testItem");
}

in the above example you can see the runItem(item)'s item is passed to promiseobject of functionB, which wen resolved returns the same in then as res, which is further used by testItem(res).

if you want to do any operation on runItem(item)'s item you can modify it before the promise is resolved for FunctionB

Shekhar Pankaj
  • 9,065
  • 3
  • 28
  • 46
  • It is an example, db.queue = Mongodb collection. From your example on what I see, I will have to pass the item through all promises as a resolve data. Is there a way that will allow me to pass item through, without putting it in resolve call. – puppeteer701 Jul 11 '16 at 11:23