Bluebird,Promise.bind - 如何在未决承诺中访问绑定上下文?

时间:2014-06-20 18:07:32

标签: promise bluebird

有人指着我在蓝鸟中Promise.bind()能够在承诺链中拥有一个上下文。

但是,我的用例要求我使用此上下文"来自外部"当我只引用promise对象(带有thencatch API的对象)时。

这可能吗?

1 个答案:

答案 0 :(得分:4)

当然,.bind有一个非静态对应物。

yourPromise.bind({x:3}).then(function(data){
     console.log("yourPromise fulfilled with", data, "and x is ", this.x);
});

这是fiddle illustrating the usage