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 = 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);
}
})
promises.push(p1);
}
if (apply) {
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'apply');
var p2 = 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);
}
})
promises.push(p2);
}
}
}).then(function (promises) {
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'promises then', promises);
return Promise.all(promises).then(function (values) {
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'promises all', 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?
via Caty Matos
No comments:
Post a Comment