Alexa SDK + NodeJS:承诺链接

时间:2018-05-24 13:47:43

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

我正在使用带有NodeJS的AWS lambda函数来获取我的alexa技能。但我坚持使用异步功能。

这是要求

  1. 在启动时,调用异步函数function1
  2. 完成function1后,结果将传递给异步function2
  3. 应向用户说明function2的结果
  4. 所以我试图通过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个异步函数时

1 个答案:

答案 0 :(得分:0)

看看是否有帮助:

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