I wanted to do something like:
somePromiseFunc(value1) .then(function(value2, callback) { // insert the next then() into this function: funcWithCallback(callback); }) .then(function(dronesYouAreLookingFor){ // Have a party }) .done();
This did not work. I could not get it to work. I was recommended to use defer() for this purpose.
Their own documents say that we should use deferred functions for callback functions. Although this is confusing because their famous smoothing pyramid example is all callbacks, but the example is too abstract to follow.
Therefore, I see a lot of people using defer , and this is what I did:
somePromiseFunc(value1) .then(function(value2) { var promise = q.defer(); funcWithCallback(function(err, dronesYouAreLookingFor){ if (!err) promise.resolve(dronesYouAreLookingFor); else promise.reject(new Error(err)); }); return promise.promise; }) .then(function(dronesYouAreLookingFor){
Until I found out about the source code, this also works:
somePromiseFunc(value1) .then(function(value2) { return function() { funcWithCallback(arguments[1]); }; }) .then(function(dronesYouAreLookingFor){
Why shouldn't I use this much simpler undocumented version?
It is undocumented because, although it seems to smooth the pyramid, return function(){withCB(arguments[1])} works, and return function(err, cb){withCB(cb)} does not work.
source share