2017-06-08 5 views
1

こんにちは皆私は約束が非同期の方法で行われているため、自分のコードに問題があります。同期が必要です。だから私は、内部Iがあれば適用し、コミットに入ってる私のログでは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); 
      }); 
} 

をfullfilled 2つの約束の値を返すために、この機能を持っています。

は、今私は、このログを受けています:

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

は、誰もが私にこの問題を解決するためにいくつかの助けを与えることができますか?

+0

コールバック最初の約束の配列を返す必要がありますか? 'createLog()'は何を返しますか? – guest271314

+0

私は約束を理解するためにp1とp2を解決する必要がありますか?私はp1を新しい約束事として作り、dataCommit [0] .resultを解決しようとしましたが、何も変わっていません...コードが変わったのを見てください –

+1

['Promise'コンストラクタ反パターンを避けてください](https://stackoverflow.com/q/23803743)/1048572?約束の建設 - 反パターン - と - 回避する方法 - それは)です! – Bergi

答えて

0

問題はありませんPromiseまたは他の値は.then()Controllers.Ubus.uciRequest()コールに実際にreturn edからです。

+0

私のコードを編集しましたpls –

+0

@CatyMatosなぜコードを編集する必要がありますか?この問題は、 '.then()'から返される値はありません。 – guest271314

+0

私はPromise.all(約束しています).then(関数(値){ –

0

その後、 `promises.push(P1)`と `promise.push(P2)`の結果を期待されている何

関連する問題