javascript 无法使用 Promise 读取未定义的属性“then”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48299936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot read property 'then' of undefined with Promise
提问by Riddle Aaron
function recursiveAsyncReadLine(){
rl.question("name : ", function(answer) {
if(answer==="exit"){
rl.close();
}
var kitty = new Kitten({name : answer});
kitty.save(function(err, kitty){
if(err){
throw err;
}
kitty.speak();
Kitten.find(function(err, kittens){
if(err){
throw err;
}
console.log(kittens);
recursiveAsyncReadLine();
});
});
});
}
I tried to change the code above with promise.
我试图用承诺更改上面的代码。
function recursiveAsyncReadLine(){
rl.question("name: ")
.then((answer)=>{
if(answer==="exit"){
rl.close();
}
var kitty = new Kitten({name : answer});
return kitty.save();
})
.then((kitty)=>{
kitty.speak();
return Kitten.find();
})
.then((kittens)=>{
console.log(kittens);
recursiveAsyncReadLine();
})
.catch((err)=>{
throw err;
});
}
But it doesn't work with a message
但它不适用于消息
TypeError: Cannot read property 'then' of undefined
类型错误:无法读取未定义的属性“then”
The error occured at the first 'then' statement.
错误发生在第一个“then”语句中。
I'm not certain that I understood Promise correctly. Where did I make a mistake?
我不确定我是否正确理解了 Promise。我在哪里做错了?
采纳答案by suresh kumar
You have misunderstood the promise. Your r1.question function does not return a promise at present. It rather accepts a callback function and then you continue async execution
你误解了承诺。您的 r1.question 函数目前没有返回承诺。它宁愿接受一个回调函数,然后你继续异步执行
In Order to promisify it, you can create a wrapper function to r1.question as follows:---
为了保证它,您可以为 r1.question 创建一个包装函数,如下所示:---
var promisifiedr1 = new Promise(function(resolve, reject){
rl.question("name: ", function(answer){
resolve(answer);
})
});
promisifiedr1.then((answer)=>{
if(answer==="exit"){
rl.close();
}
var promisifiedkitty = new Promise(function(resolve, reject){
var kitty = new Kitten({name : answer});
kitty.save(function(err, kitty){
if(err){
throw err;
}
resolve(kitty);
});
promisifiedkitty.then((kitty)=>{
kitty.speak();
return Kitten.find();
})
.then((kittens)=>{
console.log(kittens);
recursiveAsyncReadLine();
})
.catch((err)=>{
throw err;
});
});
回答by Jim Wright
It looks like question()does not return promises, but instead will call a callback. In this situation you can use promisifyas a wrapper to get a promise.
看起来question()不会返回承诺,而是会调用回调。在这种情况下,您可以使用promisify作为包装器来获得承诺。
const util = require('util');
function recursiveAsyncReadLine(){
questionPromise = util.promisify(r1.question);
questionPromise("name: ")
.then((answer)=>{
if(answer==="exit"){
rl.close();
}
var kitty = new Kitten({name : answer});
return kitty.save();
})
.then((kitty)=>{
kitty.speak();
return Kitten.find();
})
.then((kittens)=>{
console.log(kittens);
recursiveAsyncReadLine();
})
.catch((err)=>{
throw err;
});
}

