Hallo allerseits Ich habe Probleme mit meinem Code, weil Versprechen auf eine asynchrone Art und Weise gemacht werden, und ich brauche sie zu synchronisieren. Also habe ich diese Funktion den Wert der beiden Versprechen zurückzukehren innen fullfilledSync-Code für Promise Alle
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);
});
}
Durch meine Protokolle Ich Eingabe die in Anwendung und begehen, wenn.
Im Moment bin ich Empfang dieses Protokoll:
[2017-06-08 15:37:39.621] - error: /opt/wscontroller/wscontroller-api/routes/services ServicesController NA uci error promise all {}
Wer kann mir etwas Hilfe dieses Problem zu lösen?
Rückruf Was Ergebnis von 'promises.push (p1) erwartet wird,' und 'promise.push (p2)'? Was gibt 'createLog()' zurück? – guest271314
Ich brauche p1 und p2 gelöst werden, wie Versprechen verstehen? Ich habe versucht, p1 als ein neues Versprechen zu machen und zu lösen dataCommit [0] .result aber nichts geändert ... Siehe meinen Code geändert pls –
Vermeiden Sie die ['Promise' Konstruktor antipattern] (https://stackoverflow.com/q/23803743/1048572? Was-ist-die-Verheißung-Konstruktion-Antipattern-und-wie-vermeide-es! – Bergi