I have a code block with alot of nested callbacks. I am interested in code refactor to make it more readable and avoid the code bloat. Can someone please mention how can I convert the below code to promises. I want to do a comparison to see if its worth the effort applying promises across the code where such code callbacks are implemented
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);
}
});
});
});
});