I want to chain calls to different libraries using promises. In case of failure, library methods return an object describing the error, but with different fields depending the library.
In order to report any error consistently to the callee, I would like to normalize all error objects to follow a common format. But I have no idea how to do that in an elegant way using Bluebird and/or the standard promise API.
In pseudo-js here is what I have :
var methodAFromLibX_Async = Promise.promisify(...);
var methodBFromLibY_Async = Promise.promisify(...);
methodAFromLibX_Async(...)
.then(function(result) {
methodBFromLibY_Async(...)
.then(function(result) { ... })
.catch(normalizeAndSendErrorFromLibY);
})
.catch(normalizeAndSendErrorFromLibX);
The above code seems to work, but :
- I have redundant code between
normalizeAndSendErrorFromLibY
andnormalizeAndSendErrorFromLibX
- I my real use case I have to chain more than 2 calls, and the pyramid shape of the code definitely starts looking like a callback hell...
EDIT: In order to be a little more clear, here the solution I envision, but can't achieve :