在回调中使用await(Microsoft Bot Framework v4 nodejs) [英] use await inside callback (Microsoft Bot Framework v4 nodejs)

查看:86
本文介绍了在回调中使用await(Microsoft Bot Framework v4 nodejs)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从回调内部将响应发送回.

I am trying to send the response back to chatbot emulator from inside callback.

async getUserDetails(step){
    console.log("inside get userdetaiuls modeiule")
    this.userDBObject.password = step.result;

   this.userDBMethod ( async function(response){
        console.log("inside callback return");
        console.log(response);
        await step.context.sendActivity(response); // not able to do this step
        return step.endDialog();
    });
}
async userDBMethod(callback){
request.post('#', 
        {form:{key: 'hi'}}, function (error, response, body) {
        callback("done");
});
}

我得到的错误是:

(节点:17424)UnhandledPromiseRejectionWarning:TypeError:Cannot在已撤销的代理上执行获取"在D:\ LCI \ Usecases \ statementBalance \ lionsbot-src \ bot.js:384:32在Request._callback(D:\ LCI \ Usecases \ statementBalance \ lionsbot-src \ bot.js:410:17)在Request.self.callback(D:\ LCI \ Usecases \ statementBalance \ lionsbot-src \ node_modules \ request \ request.js:185:22)在Request.emit(events.js:182:13)在Request.EventEmitter.emit(domain.js:442:20)应要求.(D:\ LCI \ Usecases \ statementBalance \ lionsbot-src \ node_modules \ request \ request.js:1161:10)在Request.emit(events.js:182:13)在Request.EventEmitter.emit(domain.js:442:20)在IncomingMessage.(D:\ LCI \ Usecases \ statementBalance \ lionsbot-src \ node_modules \ request \ request.js:1083:12)在Object.onceWrapper(events.js:273:13)(node:17424)处UnhandledPromiseRejectionWarning:未处理的诺言拒绝.这由抛出异步函数引起的错误没有障碍,或者拒绝了没有处理的承诺使用.catch().(拒绝ID:1)(节点:17424)[DEP0018]DeprecationWarning:已弃用未处理的承诺拒绝.在未来,未处理的承诺拒绝将终止具有非零退出代码的Node.js进程.

(node:17424) UnhandledPromiseRejectionWarning: TypeError: Cannot perform 'get' on a proxy that has been revoked at D:\LCI\Usecases\statementBalance\lionsbot-src\bot.js:384:32 at Request._callback (D:\LCI\Usecases\statementBalance\lionsbot-src\bot.js:410:17) at Request.self.callback (D:\LCI\Usecases\statementBalance\lionsbot-src\node_modules\request\request.js:185:22) at Request.emit (events.js:182:13) at Request.EventEmitter.emit (domain.js:442:20) at Request. (D:\LCI\Usecases\statementBalance\lionsbot-src\node_modules\request\request.js:1161:10) at Request.emit (events.js:182:13) at Request.EventEmitter.emit (domain.js:442:20) at IncomingMessage. (D:\LCI\Usecases\statementBalance\lionsbot-src\node_modules\request\request.js:1083:12) at Object.onceWrapper (events.js:273:13) (node:17424) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:17424) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

所以我该如何在回调中使用await将响应发送回用户.谢谢!

So how can I use await inside callback to send response back to the user. Thanks !

推荐答案

我建议使用 Axios -用于node.js的基于Promise的HTTP客户端-而不是请求包.由于Axios是基于Promise的,因此您可以使用async/await而不是回调.生成的代码更符合BotFramework的流程.有关更多详细信息,请参见下面的代码段和 Axios文档.

I would recommend using Axios - a promise based HTTP client for node.js - rather than the request package. Since Axios is promise based, you can use async/await instead of callbacks. The resulting code falls more in line with the flow of the BotFramework. For more details, see the code snippet below and the Axios Documentation.

async getUserDetails(step){
    this.userDBObject.password = step.result;
    try {
        const res = await axios.post('#', {form:{key: 'hi'}});
        await step.context.sendActivity("Done");
    } catch (error) {
        console.log(error);
        await step.context.sendActivity("Sorry, we were not able to complete your request.");
    } 
    return step.endDialog();
}

这篇关于在回调中使用await(Microsoft Bot Framework v4 nodejs)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆