Javascript 蓝鸟承诺链此参考

Javascript 蓝鸟承诺链此参考,javascript,node.js,promise,bluebird,Javascript,Node.js,Promise,Bluebird,我正在努力学习一个具有多个函数的类。他们每个人都回报一个承诺 我的问题是,在第一次返回承诺之后,下一个函数中的这个引用不是类对象,而是全局节点对象 以下是一些示例代码: index.js: "use strict"; const debug = require("./dbg.js"); const testClass = require("./testClass.js"); const dbg = new debug(); const tc = new testClass(dbg);

我正在努力学习一个具有多个函数的类。他们每个人都回报一个承诺

我的问题是,在第一次返回承诺之后,下一个函数中的这个引用不是类对象,而是全局节点对象

以下是一些示例代码:

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;

关于如何将每个函数固定到类引用中,有什么帮助吗?

是我自己找到的:每个新承诺都必须在末尾添加.bind(self)

例如:

testClass.prototype.Test3=function(){
让自我=这个;
返回新承诺(函数(解析){
self._dbg.printDebug(3,“testClass-Test3”,“开始打印…”);
setTimeout(函数(){
self._dbg.printDebug(3,“testClass-Test3”,“已完成打印…”);
解决();
}, 2000);

}).bind(self);//afaik,Promises没有
bind
方法,并且在使用错误的
调用的函数中使用
.bind(this)
,这个
对象不会修复您的错误。您必须在这里
使用
bind
方法。然后(tc.Test3.bind(tc))
@Thomas-问题被标记为“蓝鸟”,并且确实有
.bind()
method.@Thomas你错了,蓝鸟有一个绑定方法。那为什么投反对票?我没有投票。我有一个意见,jfriend纠正了我;我很好
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

};