使用bluebird .return()方法扩展蓝鸟承诺解析值

时间:2016-02-09 00:53:48

标签: node.js promise bluebird

我试图通过使用bluebird的.return()方法来扩展承诺解决的价值。

目前我正在使用以下代码:

doSomethingAsync()              // assume resolved value is {a: 0}
    .return({b: 1})
    .then(console.log);         // prints {b: 1}

我想将result对象({b:1})扩展为doSomethingAsync已解析的值,在本例中为{a: 0},因此我可以编写如下内容:

var extend = require('extend');

doSomethingAsync()                          // assume resolved value is {a: 0}
    .return(extend(resolvedValue , {b: 1}))
    .then(console.log);                     // should print {a: 0, b: 1}

是否可以从doSomethingAsync()参数中访问{a: 0}已解决的值(在这种情况下为.return())?

1 个答案:

答案 0 :(得分:1)

正如我在评论中提到的,这不是 this.sub = this._auth.authenticated .subscribe((loggedIn) => { if (!loggedIn) { this.sub.dispose(); // doesn't work because this.sub is null this._location.replaceState('/'); this._auth.doLogout(); // emits value again in authenticated subject } }); 应该如何使用的。只需使用这样的东西:

.return

打印:const Promise = require('bluebird'); const _ = require('lodash'); Promise.resolve({a: 1}).then(obj => _.extend(obj, {b: 2})).then(console.log);