从 Ionic 2 中的递归函数返回 Promise/Type Script/Angular 2



我有异步递归函数,需要一些时间来执行。

请参阅以下两个函数

public sendAndRecivesMessage(content:string, finalMessage:Message, outMessageCount:number = 1){
        this.promise = new Promise((resolve, reject) => {
            this.doSendAndRecivesMessage(content, finalMessage, outMessageCount);
            console.log("Have to wait until above function finish executing");
            resolve (finalMessage);
        });
        return this.promise;
    }
private doSendAndRecivesMessage(content:string, finalMessage:Message, outMessageCount:number = 1){
        let newContent = IREDMART_KEYWORD + APP_IDENTITY + outMessageCount + " " + content;
        let message:Message = new Message(newContent, API_END_POINT);
        this.sendMessage(message);
        let promise = this.recieveMessage();
        promise.then((message) => {
            let inMessageCount:number = message.content.split(MSG_COUNT_SEPARATOR)[1];
            finalMessage.content += message.content.split(MSG_COUNT_SEPARATOR)[2];
            if(inMessageCount == 1){
                finalMessage.reciever = message.reciever;
                finalMessage.date = message.date;
            }
            else{
                this.sendAndRecivesMessage(content, finalMessage, outMessageCount + 1);
            }
        });
    }

在这里doSendAndRecivesMessage函数需要一些时间来执行,它是一个递归函数。

sendAndRecivesMessage函数中,我需要等到上面的函数完成执行并且必须返回finalMessage

我怎样才能做到这一点?

另外,我尝试了如下

public sendAndRecivesMessage(content:string, finalMessage:Message, outMessageCount:number = 1){
        let newContent = IREDMART_KEYWORD + APP_IDENTITY + outMessageCount + " " + content;
        let message:Message = new Message(newContent, API_END_POINT);
        this.promise = new Promise((resolve, reject) => {
            this.sendMessage(message);
            let promise = this.recieveMessage();
            promise.then((message) => {
                let inMessageCount:number = message.content.split(MSG_COUNT_SEPARATOR)[1];
                finalMessage.content += message.content.split(MSG_COUNT_SEPARATOR)[2];
                if(inMessageCount == 1){
                    finalMessage.reciever = message.reciever;
                    finalMessage.date = message.date;
                    resolve(finalMessage);
                }
                else{
                    this.sendAndRecivesMessage(content, finalMessage, outMessageCount + 1);
                }
            });
        });
        return this.promise;
    }

但是由于函数是异步和递归的,因此返回承诺不包含解析finalMessage

请给我一个建议来克服这个问题。

谢谢

你可以把整个事情想象成一列火车(一列承诺列车(,而你想调用的函数是那列火车中的货车。您所要做的就是以正确的顺序连接它们。

有两种

方法可以将货车连接到火车上。一种是对 promise 的then函数进行回调。第二种方法是在其中一个回调中返回另一个承诺。

下面是如何构建代码的示例:

function randomNumber() {
    // a function that can later return a value
    return Promise.resolve(Math.random());
}
// recursive function, keeps calling itself until the condition is met
function collectValues(until, previous) {
    // call the async function return a promise
    return randomNumber().then((value) => {
        // when we get a number we check the condition
        console.log(`prev:${previous} new:${value}`);
        let result = previous + value;
        if (result > until) {
            // when satisfied just return the final message
            return result;
        } else {
            // return the promise from the next recursive call with new params
            return collectValues(until, result);
        }
    });
}
collectValues(5, 0).then(console.log);

相关内容

  • 没有找到相关文章