bluebird Promise 链此参考
bluebird Promise chain this reference
我正在努力使用具有多种功能的 class。他们每个人 return 都是一个承诺。
我的问题是,在 Promise 的第一个 return 之后,下一个函数中的 this-reference 不是 class 对象而是全局节点对象。
下面是一些示例代码:
index.js:
"use strict";
const debug = require("./dbg.js");
const testClass = require("./testClass.js");
const dbg = new debug();
const tc = new testClass(dbg);
tc.printTestMessage()
.then(tc.Test1)
.then(tc.Test2)
.then(tc.Test3);
testClass.js:
"use strict";
const Promise = require("bluebird");
let testClass = function(dbg) {
let self = this;
self._dbg = dbg;
};
testClass.prototype.printTestMessage = function() {
let self = this;
return new Promise(function(resolve) {
self._dbg.printDebug(3, "testClass - printTestMessage", "start printing...");
setTimeout(function() {
self._dbg.printDebug(3, "testClass - printTestMessage", "finished printing...");
resolve();
}, 2000);
});
};
testClass.prototype.Test1 = function() {
let self = this;
return new Promise(function(resolve) {
self._dbg.printDebug(3, "testClass - Test1", "start printing...");
setTimeout(function() {
self._dbg.printDebug(3, "testClass - Test1", "finished printing...");
resolve();
}, 2000);
});
};
testClass.prototype.Test2 = function() {
let self = this;
return new Promise(function(resolve) {
self._dbg.printDebug(3, "testClass - Test2", "start printing...");
setTimeout(function() {
self._dbg.printDebug(3, "testClass - Test2", "finished printing...");
resolve();
}, 2000);
});
};
testClass.prototype.Test3 = function() {
let self = this;
return new Promise(function(resolve) {
self._dbg.printDebug(3, "testClass - Test3", "start printing...");
setTimeout(function() {
self._dbg.printDebug(3, "testClass - Test3", "finished printing...");
resolve();
}, 2000);
});
};
module.exports = testClass;
关于如何将每个函数坚持到 class 参考的任何帮助?
自己找的:每一个新的Promise都要在末尾加上.bind(self)
例如:
testClass.prototype.Test3 = function() {
let self = this;
return new Promise(function(resolve) {
self._dbg.printDebug(3, "testClass - Test3", "start printing...");
setTimeout(function() {
self._dbg.printDebug(3, "testClass - Test3", "finished printing...");
resolve();
}, 2000);
}).bind(self); //<-- this is the important part
};
我正在努力使用具有多种功能的 class。他们每个人 return 都是一个承诺。
我的问题是,在 Promise 的第一个 return 之后,下一个函数中的 this-reference 不是 class 对象而是全局节点对象。
下面是一些示例代码:
index.js:
"use strict";
const debug = require("./dbg.js");
const testClass = require("./testClass.js");
const dbg = new debug();
const tc = new testClass(dbg);
tc.printTestMessage()
.then(tc.Test1)
.then(tc.Test2)
.then(tc.Test3);
testClass.js:
"use strict";
const Promise = require("bluebird");
let testClass = function(dbg) {
let self = this;
self._dbg = dbg;
};
testClass.prototype.printTestMessage = function() {
let self = this;
return new Promise(function(resolve) {
self._dbg.printDebug(3, "testClass - printTestMessage", "start printing...");
setTimeout(function() {
self._dbg.printDebug(3, "testClass - printTestMessage", "finished printing...");
resolve();
}, 2000);
});
};
testClass.prototype.Test1 = function() {
let self = this;
return new Promise(function(resolve) {
self._dbg.printDebug(3, "testClass - Test1", "start printing...");
setTimeout(function() {
self._dbg.printDebug(3, "testClass - Test1", "finished printing...");
resolve();
}, 2000);
});
};
testClass.prototype.Test2 = function() {
let self = this;
return new Promise(function(resolve) {
self._dbg.printDebug(3, "testClass - Test2", "start printing...");
setTimeout(function() {
self._dbg.printDebug(3, "testClass - Test2", "finished printing...");
resolve();
}, 2000);
});
};
testClass.prototype.Test3 = function() {
let self = this;
return new Promise(function(resolve) {
self._dbg.printDebug(3, "testClass - Test3", "start printing...");
setTimeout(function() {
self._dbg.printDebug(3, "testClass - Test3", "finished printing...");
resolve();
}, 2000);
});
};
module.exports = testClass;
关于如何将每个函数坚持到 class 参考的任何帮助?
自己找的:每一个新的Promise都要在末尾加上.bind(self)
例如:
testClass.prototype.Test3 = function() {
let self = this;
return new Promise(function(resolve) {
self._dbg.printDebug(3, "testClass - Test3", "start printing...");
setTimeout(function() {
self._dbg.printDebug(3, "testClass - Test3", "finished printing...");
resolve();
}, 2000);
}).bind(self); //<-- this is the important part
};