【问题标题】:What is the other way to do a request with a promise? with nodeJS用承诺做请求的另一种方法是什么?使用 nodeJS
【发布时间】:2019-09-28 03:56:00
【问题描述】:

您好,我想将我的 watson 助手与 alexa 设备连接,为此我需要 Amazon 开发技能包和 AWS lambda。但我无法连接 watson,因为我的承诺有问题,而且我在亚马逊开发者控制台中看不到我的代码日志。我的助手负责 nodeJs 应用程序。

我尝试了一些代码:

const MyNameIsIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
      && handlerInput.requestEnvelope.request.intent.name === 'SearchIntent';
  },
   async handle(handlerInput) {


      assistant.createSession({
        assistant_id: assistant_id
      })
          .then(res => {
              session_id = res.session_id;
          })
          .catch(err => {
              console.log(err);
          });
        assistant.message({
            assistant_id: assistant_id,
            session_id: session_id,
            input: {
                'message_type': 'text',
                'text': "hello"
            }
        })
            .then(res => {
                console.log(JSON.stringify(res, null, 2));
                 speechText = res.output.generic.response.text;
            })
            .catch(err => {
              speechText = err;
      });


    }, function(err){
      speechText = "Problem with Api call";
    });

    return handlerInput.responseBuilder
      .speak(speechText)
      .getResponse();
  },
};

还有其他的承诺方式:

try{

      let res = await assistant.createSession({
        assistant_id: assistant_id
      });

      session_id = res.session_id;

      let message = await assistant.message({
        assistant_id: assistant_id,
        session_id: session_id,
        input: {
          'message_type': 'text',
          'text': "hello"
        }

      });
      speechText = message.output.generic.response.text;

    }catch(err){
      speechText = err;
    }

speechText 的结果应该是“Good day to you”,这是来自 Watson 的回应。但现在 Alexa 说“抱歉,我听不懂命令。请再说一遍。”

您是否有其他方法可以尝试使用其他方式来实现承诺?谢谢你!

【问题讨论】:

标签: node.js amazon-web-services promise ibm-watson alexa-skills-kit


【解决方案1】:

一些想法供考虑......

看看第一次尝试,handle() 中有两个独立的 Promise 链,实际上是这样的:

async handle(handlerInput) {
    doSomething_1().then(...).catch(...);
    doSomething_2().then(...).catch(...);
},

您似乎需要的是串行执行两个异步操作:

handle(handlerInput) {
    return doSomething_1().then(doSomething_2)....;
},

完整(带有一些猜测/创造性):

handle(handlerInput) {
    return assistant.createSession({ assistant_id })
    .then(res => {
        return assistant.message({
            'assistant_id': assistant_id, // ??
            'session_id': res.session_id,
            'input': {
                'message_type': 'text',
                'text': 'hello'
            }
        })
        .catch(err => {
            err.message = 'Problem with assistant.message() call';
            throw err; // send error with custom message down the down the error path.
        }
    }, err => {
        err.message = 'Problem with assistant.createSession() call';
        throw err; // send error with custom message down the down the error path.
    }
    .then(res => res.output.generic.response.text) // send res.output.generic.response.text down the success path.
    .catch(err => err.message) // catch error from anywhere above and send err.message down the success path to appear as `speechText` below.
    .then(speechText => { // `speechText` is the text delivered by the immediately preceeding .then() or .catch()
        return handlerInput.responseBuilder
        .speak(speechText)
        .getResponse();
    })
    .catch(err => { // in case handlerInput.responseBuilder.speak().getResponse() throws
        console.log(err);
    });
}

注意事项:

  1. 文本沿承诺链的成功路径传递,并在最终的.then()中显示为speechText;不需要外部变量。
  2. 自定义错误消息(如果需要)被注入中间捕获,这些捕获修改并(重要地)重新抛出它们的错误。
  3. 第三个中间捕获拦截错误路径并将 err.message 发送到成功路径。
  4. speechText 的消耗必然取决于已完成的序列 assistant.message().then(assistant.message)
  5. 可以使用 async/await 编写代码,但仅在语义上有所不同;这两个版本都会利用 Promise。

代码不一定是有效的解决方案,但应该有助于理解此应用程序所需的数据/错误流类型。

祝你好运。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    相关资源
    最近更新 更多