Q.js,promise,classes和“this”是什么意思?

我对Q诺言中的背景完全感到困惑。 我不认为这是特定的Q,而是所有的承诺一般。 this在课堂内的背景是什么?

这段代码使用TypeScript,现在一切都是静态的,因为我基本上没有做任何非静态的事情。 这段代码工作正常。

我试图添加一个private _config; 实例variables,并使用_getConfig方法在构造函数中设置_config 。 但是,当我在this._config方法中使用this._config时,它与_getConfig()方法返回的对象不是同一个对象。 (我在debugging模式下观察variables状态)

所以我猜this是在一个类里面,因为我调用Q promise的代码,没有类实例上下文。

我想知道使用promise是否是一个好主意,如果遇到上下文问题,代码将会变得更加难以理解和debugging。 我希望了解为什么,并作出select的结果。 我不想丢失类实例上下文,这太棘手了。 我害怕使用一种实际上使事情变得更加复杂的技术,我更喜欢回拨地狱

 ///<reference path='./def/defLoader.d.ts'/> export class App { /** * Constructor. * Load the config. * @return {} */ private static _getConfig(){ if(typeof __config !== "undefined"){ return __config; }else{ require('./../../shared/lib/globals/services'); return configHelper.load('_serverConfig', require('./../../shared/config/_serverConfig.json').path.config, __dirname + '/../../'); } } /** * Check that the mongoose connection open correctly, meaning that the mongod process is running on the host. * @return {Q.Promise<T>|Function} */ public static checkMongodbConnection(){ var config = App._getConfig(); // Building promise var deferred: any = Q.defer(); if(config.game.checkMongodb){ // Retrieves the mongoose configuration file, the env doesn't matter here. var mongodbConfig = require('./../../shared/config/mongodb.json')['development']; // Try mongoose connexion mongoose.connect('mongodb://' + mongodbConfig.host + '/' + mongodbConfig.database); // Bind connexion var db: mongoose.Connection = mongoose.connection; // Get errors db.on('error', function(err) { deferred.reject('Mongodb is not running, please run the mongod process: \n' + err) }); // If the connexion seems to be open db.once('open', function callback () { // Close it db.db.close(); // Resolve promise deferred.resolve(); }); }else{ deferred.resolve(); } // Get back promise return deferred.promise; } /** * Check that the redis connection is open, meaning that the redis-server process is running on the host. * @return {Q.Promise<T>|Function} */ public static checkRedisConnection(){ var config = App._getConfig(); // Building promise var deferred: any = Q.defer(); if(config.game.checkRedis) { // Create the redis client to test to connexion on server var redisClient:any = redis.createClient(); // Get client errors redisClient.on("error", function (err) { deferred.reject(err); }); // Try applying a key redisClient.set("keyTest", true); // Now key is applied, try getting it redisClient.get("keyTest", function (err, reply) { if (err) { deferred.reject("Redis is not running, please make sure to run redis before to start the server. \n" + err); } else { deferred.resolve(); } }); }else{ deferred.resolve(); } // Get back promise return deferred.promise; } } 

调用类的代码:

 Q.fcall(App.checkRedisConnection) .then(App.checkMongodbConnection) .then(function(result) { // run server }, console.error); 

Promises / A +规范明确规定,在promise链中的值总是未定义的(严格模式)或全局对象:

2.2.5 onFulfilled和onRejected必须被称为函数(即没有这个值)。

在这里指定。

如果你不使用像Bluebird这样的允诺类库(允许通过.bind明确设置),你仍然可以使用TypeScript的胖箭头(同样在ES6中)用词法来调用某些东西。