【发布时间】:2019-05-07 02:07:55
【问题描述】:
在此文档之后 http://bluebirdjs.com/docs/api/promise.bind.html 我希望
绑定上下文的生命将在调用结束时结束。但显然不是。
以下代码:
const Promise = require('bluebird');
const chain = (callNumber) => {
console.log('call:', callNumber, '============');
return asyncFunction()
.bind({})
.then(() => {
console.log('this', callNumber, this);
this.t = 1
})
.then(() => {
this.t2 = 2
})
.then(() => {
console.log('this', callNumber, this);
})
};
const asyncFunction = () => new Promise((resolve) => {
return Promise.delay(100)
.then(resolve);
});
chain(1).then(() => chain(2));
产生这个结果:
call: 1 ============
this 1 {}
this 1 { t: 1, t2: 2 }
call: 2 ============
this 2 { t: 1, t2: 2 }
this 2 { t: 1, t2: 2 }
预期结果:
call: 1 ============
this 1 {}
this 1 { t: 1, t2: 2 }
call: 2 ============
this 2 {}
this 2 { t: 1, t2: 2 }
这是正确的行为还是我在某处犯了错误?
【问题讨论】:
-
请按照您的要求添加结果
-
.bind 不适用于箭头函数——它们有词法 this
标签: javascript node.js promise bluebird