Node.js 我在使用Promise摆脱异步效应时遇到了麻烦
我正在使用node.js和mongoose制作一个应用程序,它可以帮助用户解析文本中的特定单词。但它不能同步工作。以下是我的代码:Node.js 我在使用Promise摆脱异步效应时遇到了麻烦,node.js,asynchronous,promise,Node.js,Asynchronous,Promise,我正在使用node.js和mongoose制作一个应用程序,它可以帮助用户解析文本中的特定单词。但它不能同步工作。以下是我的代码: app.post("/detector/result", function(req, res) { var result = {}; var text = req.body.paragraph; var text2 = text.replace(/,/g, ''); var text3 = text2.replace(/\./g, ''); va
app.post("/detector/result", function(req, res) {
var result = {};
var text = req.body.paragraph;
var text2 = text.replace(/,/g, '');
var text3 = text2.replace(/\./g, '');
var textArr = text3.split(" ");
new Promise(function(resolve, reject) {
textArr.forEach(function(word) {
grewords.find({
English: word
}, function(err, greword) {
if (err) {
console.log(err);
} else {
if (!(greword.length < 1 || greword == undefined)) {
console.log(word);
console.log(result);
if (word in result) {
result[word] = result[word] + 1;
} else {
result[word] = 1;
}
}
}
});
});
resolve(result);
}).then(function(r) {
console.log(r);
res.render("detectorresult", {
grewordObj: r
});
});
});
为什么
{}
仍然先显示而不是最后显示?您需要了解JavaScript的本质(异步编程)。您不能多次调用resolve
或reject
函数,您可以使用Promise.all()
函数,下面的示例可能会对您有所帮助
app.post('/detector/result', function (req, res) {
var result = {};
var text = req.body.paragraph;
var text2 = text.replace(/,/g, '');
var text3 = text2.replace(/\./g, '');
var textArr = text3.split(' ');
Promise.all(textArr.map(function (word) {
return new Promise(function (resolve, reject) {
grewords.find({
English: word
}, function (err, greword) {
if (err) {
console.log(err);
reject(err);
} else {
if (!(greword.length < 1 || greword == undefined)) {
console.log(word);
console.log(result);
if (word in result) {
result[word] = result[word] + 1;
} else {
result[word] = 1;
}
}
resolve(greword);
}
});
});
})).then(function (r) {
console.log('Final result ', r); // r will be an array of object as we rerturned object in each iteration
console.log('Final result ', result);
res.render('detectorresult', {
grewordObj: result
});
}).catch(function (err) {
console.log('Got error ', err);
res.status(500).send(err);
});
});
app.post('/detector/result',函数(req,res){
var result={};
var text=请求正文段落;
var text2=text.replace(/,/g',);
var text3=text2.替换(/\./g',);
var textArr=text3.split(“”);
Promise.all(textArr.map)(函数(word){
返回新承诺(功能(解决、拒绝){
grewords.find({
中文:单词
},函数(err,greword){
如果(错误){
控制台日志(err);
拒绝(错误);
}否则{
if(!(greword.length<1 | | greword==未定义)){
console.log(word);
控制台日志(结果);
如果(结果中的单词){
结果[字]=结果[字]+1;
}否则{
结果[字]=1;
}
}
决心(greword);
}
});
});
})).然后(函数(r){
log('Final result',r);//在每次迭代中重新返回对象时,r将是一个对象数组
console.log(“最终结果”,结果);
res.render('detectorresult'{
grewordObj:结果
});
}).catch(函数(err){
log('Got error',err);
资源状态(500)。发送(错误);
});
});
谢谢您的评论。第一个{}
来自最后一个控制台.log(r)
word
是数组中的每个元素textArr
。我想首先运行forEach
函数,然后将对象结果
传递给grewordObj
,以便在ejs文件中使用它。但事实上,它仍然无法工作,因为存在异步效应--console.log(r)
在forEach
函数完成之前首先运行。从概念上讲,在这里,您不会“摆脱异步效应”。相反,您将学习如何使用它正确编程,如何管理它,如何将异步操作与其他代码协调。这是在node.js中进行服务器编程时首先需要做的主要事情之一。感谢您的评论!非常感谢你的回答,这是非常有帮助和启发性的。另外,承诺。所有的
都非常适合我的应用程序!
app.post('/detector/result', function (req, res) {
var result = {};
var text = req.body.paragraph;
var text2 = text.replace(/,/g, '');
var text3 = text2.replace(/\./g, '');
var textArr = text3.split(' ');
Promise.all(textArr.map(function (word) {
return new Promise(function (resolve, reject) {
grewords.find({
English: word
}, function (err, greword) {
if (err) {
console.log(err);
reject(err);
} else {
if (!(greword.length < 1 || greword == undefined)) {
console.log(word);
console.log(result);
if (word in result) {
result[word] = result[word] + 1;
} else {
result[word] = 1;
}
}
resolve(greword);
}
});
});
})).then(function (r) {
console.log('Final result ', r); // r will be an array of object as we rerturned object in each iteration
console.log('Final result ', result);
res.render('detectorresult', {
grewordObj: result
});
}).catch(function (err) {
console.log('Got error ', err);
res.status(500).send(err);
});
});