2017-07-09 18 views
0

私は以下のコードを持っており、ロールバックを行うために拒否が発生したときに実行を停止したい。しかし、チェーン内に不合格があっても実行を継続します。 チェーン内の拒否にもかかわらず、チェーンされた約束が実行され続ける

__cacheHandheldData(userResult.handhelduuid).then((oldhandheldId) => { 
    olduuid = oldhandheldId; 
    __cacheUserdata(userResult.userdata_id) //userResult.userdata_id 
}).then((x) => { 
    __reconciliateToNewUserData(userResult.userdata_id, handheldData.uuid) 
}).then((x) => { 
    __linkUser(userResult._id, handheldData.userdata_id, handheldData.uuid) 
}).then((x) => { 
    __unlinkUser(olduuid) 
}).then((x) => { 
    __attachMergedUserdata(userResult.handhelduuid) 
}).then((x) => { 
    __cleanHandheld(userResult.handhelduuid) 
}).then((x) => { 
    __cleanUserdata(userResult.userdata_id) 
}).then((x) => { 
    __cleanHandheldCache(userResult.handhelduuid) 
}).then((x) => { 
    __cleanUserdataCache(userResult.userdata_id) 
}).then((x) => { 
    __removeHandHeldFromRedis(userResult.handhelduuid) 
}).then((x) => { 
    console.log('reached to destination') 
    var response = {}; 
    response.errorCode = '00'; 
    res.setHeader('Access-Control-Allow-Origin', '*'); 
    res.send(201, response); 
    return next; 
}).catch((err) => { 
    console.log('[general]', err) 
}); 

これら__functionsの各

は形式です

function __cacheHandheldData(oldhandhelduuid){ 
return new Promise((resolve, reject)=>{});} 
+3

個別の関数呼び出しでreturn文が見つかりませんでした。彼らは接続されていません。 – Sirko

答えて

2

あなたはのように.then()ハンドラでの約束を返すことがあります:あなたは内からの約束を返す場合にのみ

}).then((x) => { 
    return __reconciliateToNewUserData(userResult.userdata_id,handheldData.uuid) 
// ^^^^^^ add return here to return the promise 
}).then((x) => { 

.then()ハンドラは実際にチェーンに追加されます。それがなければ、それは親チェーンに全く接続していない独立した約束チェーンです。 .then()ハンドラには、それらの中で実行される非同期操作が完了したときに知るべき魔力がありません。彼らが知る唯一の方法は、非同期操作を表す約束を返す場合です。

上記のように約束を返すことで、チェーンの一部になった後、返された約束が拒否された場合、チェーン全体を中止します(.catch()まで)。

関連する問題