Skip to content Skip to sidebar Skip to footer

Node.js Resolve Promise And Return Value

I use the Microsoft bot framework to come up with a 'simple' PoC bot. I used a tutorial as a basis and extend it. I've a couple of basic functions for differet intents (ie. greetin

Solution 1:

I think that the problem is that your getReqStatus isn't really returning anything. In your example getGreetings function you're actually returning Promise.resolve(answers) as the return value of that function.

However, in your getReqStatus function, you just set up a listener lineReader close event:

lineReader.on('close', function(line) {
if (output == '') {
    output.push( `I was not able to find a request like ${request_number}.`);
}
    console.log(output); // list outputreturn Promise.resolve(output); 
});

You're returning a Promise resolved inside the anonymous callback function you're passing to lineReader.on() as second parameter. That is not the return value from the getReqStatus function itself, so that getReqStatus is not returning anything, as expected.

The code of that function runs correctly as standalone code, as you say, just because it sets the listener properly and it does what it has to do. However, that code just doesn't return a Promise when wrapped in a function.

What you would need is to return a Promise that wraps the lineReader.on close handler, like:

functiongetReqStatus(){
//...codereturnnewPromise( function(resolve , reject ){
    lineReader.on('close', function (line) {
        if (output == '') {
            output.push( `I was not able to find a request like ${request_number}.`);
        }
        console.log(output); // list outputreturnresolve(output);
        });
    });
}

I say would because I really don't know if this code will work, I don't have any kind of experience with the Microsoft Bot framework and not used at all with the readline module. However, even if this doesn't solve your problem, I hope it will help you a bit understanding why your function doesn't return a Promise and how could you fix it.

Post a Comment for "Node.js Resolve Promise And Return Value"