Node.js resolve a promise and return a value

I use the Microsoft bot framework to create a "simple" PoC bot. I used tutorial as a base and expanded it.

I have a couple of basic functions for different purposes (i.e. greetings, goodbye, etc.) and one with some logic in it (reqstatus).

Simple (i.e. greeting.js) return the answer beautifully, but it is not more complex (reqstatus.js). Running the main code reqstatus.js (without the first "const getReqStatus = (entity) => {") in a stand-alone script works.

server.js (main) β†’ see the call in "if (intent) {" ...

const getFeelings = require('./intents/feelings.js')
const getGoodbyes = require('./intents/goodbyes.js')
const getGreetings = require('./intents/greetings.js')
const getHelp = require('./intents/help.js')
const getReqStatus = require('./intents/reqstatus.js')
... 
const bot = new builder.UniversalBot(connector)

// Intents based on definitions on recast
const INTENTS = {
  feelings: getFeelings,
  goodbyes: getGoodbyes,
  greetings: getGreetings,
  help: getHelp,
  reqstatus: getReqStatus,
}

// Event when Message received
bot.dialog('/', (session) => {
  recastClient.textRequest(session.message.text)
  .then(res => {
  const intent = res.intent()
  const entity = res.get('request_number')

  console.log(`UserName: ${session.message.user.name}`)
  console.log(`Msg: ${session.message.text}`)
  console.log(`Intent: ${intent.slug}`) 

  if (intent) {
    INTENTS[intent.slug](entity)
    .then(res => session.send(res)) 
    .catch(err => session.send(err))
  }
  })
  .catch(() => session.send('Sorry I didn\'t get that. '))
  })
  ...

greetings.js -> Returns the string ok

const getGreetings = () => {
  const answers = ['Hi, my name is SuperBot. Nice to meet you!', ] 
  return Promise.resolve((answers))
  }
module.exports = getGreetings

reqstatus.js β†’ Does not return anything

const getReqStatus = (entity) => {
  var request = require('request');
  var request_number = entity.toLowerCase()
  var output = [];


  // Processing
  var lineReader = require('readline').createInterface({
  input: fs.createReadStream('netreqs.csv')
  }); 

  lineReader.on('line', function (line) {
  var jsonFromLine = {};
  var lineSplit = line.split(';');
  jsonFromLine.req = lineSplit[0];
  jsonFromLine.req_count = lineSplit[1];
  jsonFromLine.req_type = lineSplit[2];
  //...
  var req_lowever = jsonFromLine.req.toLowerCase()
  if (req_lowever == request_number) {
     output.push( `Your request ${jsonFromLine.req} was received`);    
  // simplified
  }
  });

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

getReqStatus , . googling . .

+4
1

, , getReqStatus . getGreetings Promise.resolve(answers) .

getReqStatus listReader close listener:

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

Promise , lineReader.on() . getReqStatus, getReqStatus , .

, , , , , . Promise .

Promise, lineReader.on, :

function getReqStatus(){
//...code

return new Promise( 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 output
        return resolve(output);
        });
    });
}

, , , Microsoft Bot readline . , , , , Promise .

+3

Source: https://habr.com/ru/post/1665666/


All Articles