【问题标题】:Providing previous values of promises提供先前的承诺值
【发布时间】:2016-07-24 11:40:03
【问题描述】:

我的问题涉及承诺和提供链承诺的先前值。问题是,项目是否从第一个返回承诺传递到第二个承诺“runItem --> testItem”?

或者我们是否必须通过所有承诺传递项目?

例子:

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});
}

编辑: 也许这是一个更好的例子:

我们可以访问原始属性项,还是在下次调用函数时会被覆盖?

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');  
  });
}

【问题讨论】:

  • 你应该返回承诺对象。
  • 我不明白你想用你的代码做什么,但是不,以前的值没有被存储,如果你需要它们,你必须保存它们(在某些情况下它很方便使用 bluebird promise 绑定到对象的能力。
  • 我想将“item”传递给testItem函数,但“item”是runItem函数中的一个属性。代码是通用的,主要是我只想知道,如果有可能,如果没有,将变量从一个承诺传递到另一个承诺的最佳方式是什么。
  • 也许可以,但我更喜欢我的示例的解决方案,因为我项目中的大部分代码都是这样组织的。

标签: javascript promise bluebird


【解决方案1】:

您可以更改先前承诺的返回值并简单地返回它。下一个承诺会受到影响。检查这个例子:

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.

【讨论】:

  • Promise.resolve({data: 7}),不需要构造函数。
【解决方案2】:

试试Promise.props函数。

有点像

...
{
    ...
    return Promise.props({res: res, item: item}).
        .then(function(result){
            return Promise.resolve({ res: res, item: item }); 
        });
}

【讨论】:

    【解决方案3】:

    您发布的示例未返回 promise 对象。

    下面是promise中的链接示例,看看吧。

    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");
    }
    

    在上面的例子中你可以看到runItem(item)item被传递给functionBpromiseobject,在then中解析返回的结果与res相同,进一步使用testItem(res).

    如果您想对runItem(item)item 进行任何操作,您可以在为FunctionB 解析promise 之前对其进行修改

    【讨论】:

    • 就是一个例子,db.queue = Mongodb 集合。从您所看到的示例中,我将不得不通过所有承诺将项目作为解析数据传递。有没有一种方法可以让我通过项目而不将其置于解析调用中。
    猜你喜欢
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多