1

Hi everyone I'm having troubles with my code because of promises are being done in a async way and i need them to be sync. So I have this function to return the value of the two promises inside fullfilled

 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);
            });
}

By my logs I'm entering in the apply and commit if.

Right now i'm receiving this log:

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

Anyone can give me some help to solve this problem?

  • What is expected result of `promises.push(p1)` and `promise.push(p2)`? What does `createLog()` return? – guest271314 Jun 08 '17 at 14:55
  • I need p1 and p2 to be resolved as promises understand? I tried to make p1 as a new promise and resolve dataCommit[0].result but nothing changed... See my code changed pls –  Jun 08 '17 at 15:09
  • 1
    Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Jun 08 '17 at 15:56

2 Answers2

0

The issue is no Promise or other value is actually returned from .then() chained to Controllers.Ubus.uciRequest() calls.

guest271314
  • 1
  • 15
  • 104
  • 177
  • I edited my code see now pls –  Jun 08 '17 at 15:09
  • @CatyMatos Why do you need to edit code? The issue is no value is returned from `.then()` – guest271314 Jun 08 '17 at 15:10
  • I'm doing Promise.all(promises).then(function (values) { –  Jun 08 '17 at 15:21
  • The updated code still does not `return` a value from `.then()` see [Why is value undefined at .then() chained to Promise?](https://stackoverflow.com/questions/44439596/why-is-value-undefined-at-then-chained-to-promise) – guest271314 Jun 08 '17 at 15:26
0

You must return the promises array in first then callback

riyaz-ali
  • 8,677
  • 2
  • 22
  • 35