簡體   English   中英

Alexa SDK + NodeJS:承諾鏈

[英]Alexa SDK + NodeJS : Promise Chaining

我的Alexa技能是將AWS lambda函數與NodeJS一起使用。 但我被困在使用異步功能。

這是要求

  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個異步功能時

看看這是否有幫助:

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的區別, 請單擊此處

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM