150
rated 0 times
[
153]
[
3]
/ answers: 1 / hits: 16448
/ 7 Years ago, wed, january 17, 2018, 12:00:00
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
The error occured at the first 'then' statement.
I'm not certain that I understood Promise correctly. Where did I make a mistake?
More From » javascript