bluebird 承诺——履行顺序不正确?
bluebird promises -- incorrect order of fulfilment?
在我的 expressJS 应用程序中,我正在尝试 运行 2 个使用 thennable 的序列中的异步承诺返回函数 ...
connect2Redis(config.cache)
.then(connect2db())
.then(function() {
console.log("Accounting Server started successfully!");
})
})
.catch(function(e){
console.error("Failed to start Accounting server. Error: ", e);
})
'connect2Redis'和'connect2db'两个fns实现如下:
function connect2Redis(opts) {
var connectingMsg = "Connecting to Redis Cache @" + config.cache.host + ":" +config.cache.port;
process.stdout.write(connectingMsg);
return new Promise(function(resolve, reject){
var redisClient = redis.createClient(opts);
redisClient.on('ready', function(){
console.log(" [ " + "OK".green + " ]");
return resolve(redisClient);
})
redisClient.on('error', function(e){
console.log("[" + "FAIL".red + "]");
return reject(e);
})
});
}
function connect2db() {
process.stdout.write("Synchronizing to database...");
return DB.sequelize.sync().then(function() {
console.log(" [ " + "OK".green + " ]");
}).catch(function(e) {
console.log("[" + "FAIL".red + "]");
return e;
});
}
我希望这两个 fns 能够“一个接一个”地执行,因为我已经使用 thennable 将它们链接起来了。有两种日志语句,一种是连接前,一种是连接后。如果一切顺利的话,我应该会看到这样的日志...
Connecting to Redis Cache @localhost:6379 [ OK ]
Synchronizing to database... [ OK ]
Accounting Server started successfully!
但是显示混乱了,我看到了这样的东西。
Connecting to Redis Cache @localhost:6379Synchronizing to database...
[ OK ]
Accounting Server started successfully!
[ OK ]
我期望 conenct2Redis fn 中的日志语句应该被打印而不会被来自 connect2db 的日志打断,因为这个第二个 fn 是在 connect2Redis 完成后调用的,使用 then(...)
这里有我遗漏的东西吗?
您需要传递对函数的引用而不是调用函数
connect2Redis(config.cache)
.then(connect2db)
在我的 expressJS 应用程序中,我正在尝试 运行 2 个使用 thennable 的序列中的异步承诺返回函数 ...
connect2Redis(config.cache)
.then(connect2db())
.then(function() {
console.log("Accounting Server started successfully!");
})
})
.catch(function(e){
console.error("Failed to start Accounting server. Error: ", e);
})
'connect2Redis'和'connect2db'两个fns实现如下:
function connect2Redis(opts) {
var connectingMsg = "Connecting to Redis Cache @" + config.cache.host + ":" +config.cache.port;
process.stdout.write(connectingMsg);
return new Promise(function(resolve, reject){
var redisClient = redis.createClient(opts);
redisClient.on('ready', function(){
console.log(" [ " + "OK".green + " ]");
return resolve(redisClient);
})
redisClient.on('error', function(e){
console.log("[" + "FAIL".red + "]");
return reject(e);
})
});
}
function connect2db() {
process.stdout.write("Synchronizing to database...");
return DB.sequelize.sync().then(function() {
console.log(" [ " + "OK".green + " ]");
}).catch(function(e) {
console.log("[" + "FAIL".red + "]");
return e;
});
}
我希望这两个 fns 能够“一个接一个”地执行,因为我已经使用 thennable 将它们链接起来了。有两种日志语句,一种是连接前,一种是连接后。如果一切顺利的话,我应该会看到这样的日志...
Connecting to Redis Cache @localhost:6379 [ OK ]
Synchronizing to database... [ OK ]
Accounting Server started successfully!
但是显示混乱了,我看到了这样的东西。
Connecting to Redis Cache @localhost:6379Synchronizing to database... [ OK ]
Accounting Server started successfully!
[ OK ]
我期望 conenct2Redis fn 中的日志语句应该被打印而不会被来自 connect2db 的日志打断,因为这个第二个 fn 是在 connect2Redis 完成后调用的,使用 then(...)
这里有我遗漏的东西吗?
您需要传递对函数的引用而不是调用函数
connect2Redis(config.cache)
.then(connect2db)