1
ネストされたコールバックがたくさんあるコードブロックがあります。私は、コードリファクタを読みやすくし、コードを膨らませないようにすることに興味があります。誰かが下のコードを約束に変換する方法を教えてください。私はそのようなコードのコールバックがで約束のあるコードで後続のコールバックを回避する方法
dataApi.getClientEntityByCtnId(Number(key), function (error, client1) {
if (error) return callback(error, null);
if (client1.id == 0)
return callback("unregistered client:"+ key, null);
dataApi.getClientEntityByCtnId(Number(key2), function (error, client2) {
if (error)
return callback(error, null);
if (client2.id == 0)
return callback("unregistered client:" + key2, null);
dataApi.setClientRelationshipEntity(client1.id, client2.id, function (error) {
if (error) return callback(error, null);
dataApi.setClientRelationshipEntity(client2.id, client1.id, function (error) {
nbCRpushed++;
if (nbCRpushed%1000==0) dataApi.logger.info("nb CR pushed into BC ="+nbCRpushed+"/" + dataApi.nbCR);
if (error) return callback(error, null);
if (nbCRpushed == dataApi.nbCR)
{
dataApi.pushCRToCache(callback);
}
});
});
});
});
[既存のコールバックAPIを約束に変換するにはどうすればいいですか?](https://stackoverflow.com/q/22519784/1048572)を読んでいますか? – Bergi