【问题标题】:Ecmascript 6 recursive function with promise具有承诺的 Ecmascript 6 递归函数
【发布时间】:2017-04-08 13:04:48
【问题描述】:

我正在尝试为 sequelize.js 编写一个基类。这个类将关联所有相关的表。 includeFk 函数实现了这个任务。但它有一个承诺,应该是递归的。 类:

class base {
    constructor(table, depth) {
        this._table = table;
        this._depth = depth;

    }

    includeFK(table, depth, includes) {
        return new Promise((resolve, reject) => {
                if (depth <= this._depth) {
                    for (var att in table.tableAttributes) {

                        table.belongsTo(m, {
                            as: m.name,
                            foreignKey: att
                        })
                        includes.push({
                            model: m
                        });

                    }
                }

                Promise.all(

                    Object.keys(table.associations).forEach(tbl => {
                            this.includeFK(table.associations[tbl].target, depth + 1, includes);
                        }

                    )).then(results => {
                    resolve(true)
                });
            } else {
                resolve(true);
            }
        });



    all(query) {
        return new Promise((resolve, reject) => {
            var tmp = this;
            var includes = [];

            Promise.all([
                this.includeFK(tmp._table, 1, includes),
                this.includeLang()
            ]).then(function() {

                tmp._table.findAll({
                    include: includes
                }).then(function(dbStatus) {
                    resolve(dbStatus);
                });
            });
        });
    }
}

错误:

(node:25079) UnhandledPromiseRejectionWarning: 未处理的承诺 拒绝(拒绝 id:3):TypeError:无法读取属性 未定义的“符号(Symbol.iterator)”(节点:25079) DeprecationWarning:不推荐使用未处理的承诺拒绝。在 未来,未处理的承诺拒绝将终止 具有非零退出代码的 Node.js 进程。 (节点:25079) UnhandledPromiseRejectionWarning:未处理的承诺拒绝 (拒绝 id:4):TypeError:无法读取属性 'Symbol(Symbol.iterator)' of undefined

【问题讨论】:

    标签: javascript node.js ecmascript-6 sequelize.js


    【解决方案1】:

    您可以处理来自 Promise.all 的错误,因为它还返回一个承诺,您需要处理它,除非您将它训练为返回的承诺。

    Promise.all([...])
        .then(...)
        .catch(function(err) {
            console.error(err);
            reject(err);
        });
    

    编辑:

    var promiseArr = [];
    
    Object.keys(table.associations).forEach(tbl => {
      promiseArr.push(
        self.includeFK(table.associations[tbl].target, depth + 1, includes)
      );
    });
    
    Promise.all(promiseArr)
      .then(results => {
        resolve(true)
      });
    

    我还认为您的 this 绑定不在正确的范围内。如果遇到未定义函数的错误,请在调用类函数之前尝试使用变量引用this

    例子:

    includeFK(table, depth, includes) {
        var self = this; //ref this and use it later
      ...
          ...
               self.includeFK();
    

    【讨论】:

    • 它只处理错误。但我的问题是 promise 不适用于递归函数。
    • 第一个问题是如果你不处理promise中抛出的错误,你就无法得到有意义的错误日志。我认为函数范围有问题,因此递归函数无法正常工作。
    • 现在错误日志是:TypeError: Cannot read property Symbol(Symbol.iterator)' of undefined at Function.all (native)
    • Promise.all 只接受迭代器,你必须为其提供一个承诺数组。为你更新了答案。
    猜你喜欢
    • 2015-12-05
    • 2018-12-01
    • 2017-05-09
    • 2019-05-25
    • 2017-12-18
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多