node.js解析promise并返回值

编程入门 行业动态 更新时间:2024-10-24 10:25:15
本文介绍了node.js解析promise并返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用Microsoft bot框架来提出一个简单的PoC机器人。我使用教程作为基础并进行扩展。

I use the Microsoft bot framework to come up with a "simple" PoC bot. I used a tutorial as a basis and extend it.

我有几个基本函数用于不同意图(即问候,再见等)和一个有更多逻辑的函数(reqstatus)。

I've a couple of basic functions for differet intents (ie. greetings, goodbye, etc) and one with some more logic in it (reqstatus).

简单的(即greeting.js)很好地返回答案,但更复杂的答案(reqstatus.js)。在独立脚本中运行reqstatus.js的主代码(没有第一个const getReqStatus =(entity)=> {)。

The simple ones (ie greeting.js) return the answer nicely but the more complex one doesn't (reqstatus.js). Running the main code of reqstatus.js (without the first "const getReqStatus = (entity) => {") in a standalone script works.

服务器。 js (主要) - >请参阅if(intent){...

server.js (main) -> see 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 - >返回字符串ok

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 - >不返回任何内容

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放入一个功能,但也没有用。 经过大量的尝试和谷歌搜索,我仍然卡住了,并想在这里问专家。非常感谢提前。

I also tried to put getReqStatus in a function but that also didn't work. After a lot of trying and googling I'm still stuck and wanted to ask the experts here. Thanks a lot in advance.

推荐答案

我认为问题在于您的 getReqStatus 并没有真正归还任何东西。在您的示例 getGreetings 函数中,您实际上返回 Promise.resolve(answers)作为该函数的返回值。

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.

但是,在你的 getReqStatus 函数中,你只需设置一个监听器lineReader close 事件:

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

你在匿名回调函数中返回一个已解决的Promise 你'重新传递给 lineReader.on()作为第二个参数。这不是 getReqStatus 函数本身的返回值,因此 getReqStatus 没有按预期返回任何内容。

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.

正如你所说,该函数的代码作为独立代码正确运行,只是因为它正确地设置了监听器并且它完成了它必须做的事情。但是,当函数包装时,该代码不会返回Promise。

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.

你需要的是返回一个包装的Promise lineReader.on 关闭处理程序,如:

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

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以及如何解决它。

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.

更多推荐

node.js解析promise并返回值

本文发布于:2023-06-04 19:13:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/503401.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:返回值   node   js   promise

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!