【问题标题】:Alexa SDK + NodeJS : Promise ChainingAlexa SDK + NodeJS:承诺链
【发布时间】:2018-11-03 18:43:25
【问题描述】:

我将 AWS lambda 函数与 NodeJS 一起用于我的 Alexa 技能。但我坚持使用异步功能。

这是要求

  1. 启动时,会调用异步函数function1
  2. function1完成后,将结果传递给异步function2。
  3. function2 的结果应该告诉用户

所以我试图通过使用Promise 链接来解决这个问题。下面是我的伪代码

function function1() {
    return new Promise((resolve, reject) => {
        //some async operation
        if (result1) {
            resolve(result1);
        } else {
            reject(error);
        }
    });
}

function function2(result1) {
    return new Promise((resolve, reject) => {
        //some async operation
        if (result2) {
            resolve(result2);
        } else {
            reject(error);
        }
    });
}

const mainHandler = {
    'LaunchRequest': function () {
        function1()
            .then(result1 => function2(result1))
            .then(result2 => {
                //succcess
                this.response.cardRenderer(result2);
                this.response.speak(result2);
                this.response.listen(config.HELP_REPROMPT);
                this.emit(':responseReady');
            }).catch(err => {
                //error
                this.response.cardRenderer(err);
                this.response.speak(err);
                this.response.listen(config.HELP_REPROMPT);
                this.emit(':responseReady');
            });
    },
};

现在我面临的问题是,alexa 在第一个函数执行后提前终止,而没有等待 function2 被执行。不知道我在这里做错了什么。任何帮助,将不胜感激。提前致谢。

注意:如果只有一个异步函数,那么它可以正常工作。即下面的代码工作正常。

const mainHandler = {
    'LaunchRequest': function () {
        function1()
            .then(result1 => {
                //succcess
                this.response.cardRenderer(result1);
                this.response.speak(result1);
                this.response.listen(config.HELP_REPROMPT);
                this.emit(':responseReady'); 
            })
            .catch(err => {
                //error
                this.response.cardRenderer(err);
                this.response.speak(err);
                this.response.listen(config.HELP_REPROMPT);
                this.emit(':responseReady');
            });
    },
};'

问题在于涉及 2 个异步函数时

【问题讨论】:

    标签: javascript node.js aws-lambda alexa alexa-skills-kit


    【解决方案1】:

    看看这是否有帮助:

    function function1() {
        return new Promise((resolve, reject) => {
            //some async operation
            if (result1) {
                resolve(result1);
            } else {
                reject(error);
            }
        });
    }
    
    function function2(result1) {
        return new Promise((resolve, reject) => {
            //some async operation
            if (result2) {
                resolve(result2);
            } else {
                reject(error);
            }
        });
    }
    
    const ErrorHandler = {
      canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
          && handlerInput.requestEnvelope.request.intent.name === 'LaunchRequest';
      },
      async handle(handlerInput, error) {
        result1 = await function1();
        result2 = await function2(result1);
        /* rest of code according to version2 */
            },
        };
    

    对于 v1 和 v2 sdk 的区别click here.

    【讨论】:

    • 我刚开始的时候那个文档在哪里哈哈
    猜你喜欢
    • 2020-04-24
    • 2015-10-03
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 2019-03-19
    • 1970-01-01
    • 2020-06-04
    相关资源
    最近更新 更多