最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot read property 'then' of undefined with Promise - Stack Overflow

programmeradmin4浏览0评论
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?

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?

Share Improve this question asked Jan 17, 2018 at 11:23 Riddle AaronRiddle Aaron 731 gold badge2 silver badges8 bronze badges 6
  • 1 what does question() return? – Davin Tryon Commented Jan 17, 2018 at 11:25
  • how question is defined? – Bharadwaj Commented Jan 17, 2018 at 11:25
  • 1 question() is from readline.createInterface. Sorry for missing! – Riddle Aaron Commented Jan 17, 2018 at 11:30
  • rl.question is a built in node method, but it doesn't return a Promise. Cant be chained using a then. Here is a relevant question stackoverflow./questions/42080551/… – surajck Commented Jan 17, 2018 at 11:30
  • Is at least one Promise object needed for Promise chaining? – Riddle Aaron Commented Jan 17, 2018 at 11:33
 |  Show 1 more ment

2 Answers 2

Reset to default 2

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

In Order to promisify it, you can create a wrapper function to r1.question as follows:---

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;
    });
});

It looks like question() does not return promises, but instead will call a callback. In this situation you can use promisify as a wrapper to get a promise.

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;
    });
}
发布评论

评论列表(0)

  1. 暂无评论