Node.js Nodejs承诺,在前一个完成之前启动函数
也许我不理解承诺,为什么在功能结束前我会被重定向Node.js Nodejs承诺,在前一个完成之前启动函数,node.js,promise,bluebird,Node.js,Promise,Bluebird,也许我不理解承诺,为什么在功能结束前我会被重定向 app.get('/dionic/scan', function(req, res) { var functions = require("./functions.js"); var scrape = require("./scrapers/dionic.js"); //render index.ejs file scrape.scrapeDio // Remove duplicates in array data .then(dedupe) /
app.get('/dionic/scan', function(req, res) {
var functions = require("./functions.js");
var scrape = require("./scrapers/dionic.js");
//render index.ejs file
scrape.scrapeDio
// Remove duplicates in array data
.then(dedupe)
// Save
.then(functions.dioNic)
.then(console.log("Guardado"))
.then(res.redirect('/'));
});
调用这些函数并将其返回值传递给
,然后
,而不是传递函数本身:
.then(console.log("Guardado"))
.then(res.redirect('/'));
这实际上是同一个问题。可以使用获取参数绑定的函数:
.then(console.log.bind(console, "Guardado"))
.then(res.redirect.bind(res, '/'));
如果您不喜欢bind的外观,也可以使用匿名函数:
.then( function () { console.log("Guardado") })
.then( function () { res.redirect('/') });