【问题标题】:Sync code for Promise All [duplicate]Promise All 的同步代码 [重复]
【发布时间】:2017-06-08 23:26:07
【问题描述】:

大家好,我的代码有问题,因为 promise 是以异步方式完成的,我需要它们同步。所以我有这个函数来返回 fullfilled 里面的两个 promise 的值

 ServicesController.prototype.uci = function (device, config, path, section, property, value, apply, commit) {
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'inicio');

    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'config', config);
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'path', path);
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'section', section);
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'option', property);
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'value', value);
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'apply', apply);
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'commit', commit);

    var values = {};
    values[property] = value;
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'values', values);

    return Controllers.Ubus.uciRequest('set', {"config": config, "section": section, values}, device)
            .then(function (uciData) {
                createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'uciData', uciData);
                var promises = [];

                if (uciData != null) {
                    createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'depois do if do uciData');

                    if (commit) {
                        createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'commit');
                        var p1 = new Promise(function (resolve, reject) {
                            Controllers.Ubus.uciRequest('commit', {"config": config}, device)
                                .then(function (dataCommit) {
                                    if (dataCommit && dataCommit.hasOwnProperty('result') && dataCommit.result[0] == 0) {
                                        createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'commit data', dataCommit);
                                        resolve(dataCommit.result[0]);
                                    } else {
                                        reject("no data");
                                    }
                                }).catch(function (err) {
                                    createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'error promise commit', err);
                                });
                        });                         
                        promises.push(p1);
                    }

                    if (apply) {
                        createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'apply');
                        var p2 = new Promise(function (resolve, reject) {
                                Controllers.Ubus.fileExec(device.id, "exec", path, "restart")
                                .then(function (dataApply) {
                                    if (dataApply && dataApply.hasOwnProperty('result') && dataApply.result[0] == 0) {
                                        createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'apply data', dataApply);
                                        resolve(dataApply.result[0]);
                                    } else {
                                        reject("no data");
                                    }
                                }).catch(function (err) {
                                    createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'error promise apply', err);
                                });
                        });                         
                        promises.push(p2);
                    }
                    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'promises ', promises);
                }
            }).then(function (promises) {
                createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'promises then', promises);
                Promise.all(promises).then(function (values) {
                    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'promises all', values);
return(values);

                }).catch(function (err) {
                    createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'error promise all', err);
                });
            }).catch(function (err) {
                createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'error then 2', err);
            });
}

根据我的日志,我输入了申请并提交 if。

现在我正在收到此日志:

 [2017-06-08 15:37:39.621] - error: /opt/wscontroller/wscontroller-api/routes/services ServicesController NA uci error promise all {}

谁能帮我解决这个问题?

【问题讨论】:

  • promises.push(p1)promise.push(p2) 的预期结果是什么? createLog() 返回什么?
  • 我需要 p1 和 p2 像 Promise 理解的那样被解析?我试图将 p1 作为一个新的承诺并解决 dataCommit[0].result 但没有任何改变...请参阅我的代码更改

标签: javascript node.js asynchronous


【解决方案1】:

问题是没有Promise 或其他值实际上是returned 从.then() 链接到Controllers.Ubus.uciRequest() 调用。

【讨论】:

  • 我编辑了我的代码,现在请看
  • @CatyMatos 为什么需要编辑代码?问题是 .then() 没有返回值
  • 我在做 Promise.all(promises).then(function (values) {
  • 更新后的代码仍然没有return来自.then()的值见Why is value undefined at .then() chained to Promise?
【解决方案2】:

你必须先返回 promises 数组,然后回调

【讨论】:

    猜你喜欢
    • 2018-07-08
    • 2013-01-28
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    • 2018-03-12
    • 1970-01-01
    相关资源
    最近更新 更多