蓝鸟承诺 – 不正确的履行顺序?

在我的expressJS应用程序中,我正在尝试运行2asynchronous承诺函数使用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); }) 

两个fns的“connect2Redis”和“connect2db”实现如下:

 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链接了他们。 在连接之前和连接之后有两个日志语句。 如果一切顺利,我应该看到这样的日志…

连接到Rediscaching@localhost:6379 [OK]

正在同步到数据库… [确定]

记帐服务器启动成功!

但显示混乱起来,我看到这样的事情。

连接到Rediscaching@localhost:6379同步到数据库… [确定]

记帐服务器启动成功!

[ 好 ]

我期望conenct2Redis fn中的日志语句应该被打印而不会被来自connect2db的日志打断,因为第二个fn在connect2Redis完成后调用,然后使用(…)

有什么我在这里失踪?

您需要传递函数的引用,而不是调用函数

 connect2Redis(config.cache) .then(connect2db)