javascript递归类:未定义的方法

javascript recursive class: undefined method

本文关键字:方法 未定义 递归 javascript      更新时间:2023-09-26

我有一个JavaScript类,用来帮助处理promise。首先,您将函数添加到数组中,然后它执行这些函数,弹出它们并调用自己来执行下一个函数。在数组的末尾,它解决了这个问题。我的希望是将该解决方案一直传播到递归调用的堆栈中。这将允许您使用一组简单的命令强制多个异步函数按顺序运行。此外,使用逻辑来修改ansync函数的流。

function Sequencer() {
    this.functionSequence = [];
    this.addFunction = function (func) {
        this.functionSequence.push(func);
    }
    this.getFunctionSequence = function () {
        return functionSequence;
    }
    this.executeAll = function () {
        var functionList = this.functionSequence;
        var deferred = $q.defer();
        if (functionList.length > 0) {
            functionList[0]().then(function (result) {
                if (result) {
                    functionList.splice(0, 1);
                    executeAll().then(function (resultInner) {
                        if (resultInner == true) {
                            deferred.resolve(true);
                        } else {
                            deferred.resolve(false);
                        }
                    });
                } else {
                    functionList = [];
                    deferred.resolve(false);
                }
            });
        } else {
            deferred.resolve(true);
        }
        return deferred.promise;
    }
}

我正在获取ReferenceError:"executeAll"未定义在这个脚本中,在拼接之后的递归调用行"executeAll"上

数组中的第一个函数正在执行(我用模态弹出窗口测试它),当它解析时,它会碰到拼接,然后它会在executeAll行上抛出错误。我对函数的定义有误吗?作为递归函数,我是否正确地调用了它?

使用this.executeAll-假设this是正确的,但事实并非如此,因此您也需要考虑到这一点。。。类似于executeAll顶部的var self = this,然后调用self.executeAll

this.executeAll = function() {
    var functionList = this.functionSequence;
    var deferred = $q.defer();
    var self = this; // save reference to this
    if (functionList.length > 0) {
        functionList[0]().then(function(result) {
            if (result) {
                functionList.splice(0, 1);
                // need to use self here because "this" is not the "this" we want
                self.executeAll().then(function(resultInner) {
                    if (resultInner == true) {
                        deferred.resolve(true);
                    } else {
                        deferred.resolve(false);
                    }
                });
            } else {
                functionList = [];
                deferred.resolve(false);
            }
        });
    } else {
        deferred.resolve(true);
    }
    return deferred.promise;
};

this不是你"想要"的this的原因是this在javascript中的工作方式-关于使用this的堆栈交换有很多信息-我很快就会找到并链接一个好的答案


我提供这个替代代码

this.executeAll = function() {
    return this.functionSequence.reduce(function(promise, item) {
        return promise.then(function(result) {
            if (result) {
                return item();
            }
            else {
                throw "Fail"; // throw so we stop the chain
            }
        });
    }, Promise.resolve(true))
    .then(function(result) {
        this.functionSequence = []; // clear out the added functions
        return true; // fulfilled value is true as per original code
    }.bind(this), function(err) {
        this.functionSequence = []; // clear out the added functions
        if (err == "Fail") {
            return false; // convert the "Fail" to a fullfilled value of false as per original code
        }
        else {
            throw err; // any other error - re-throw the error
        }
    }.bind(this))
};