Last active
March 11, 2016 10:48
-
-
Save tung-dang/49896fd4c5d384df0254 to your computer and use it in GitHub Desktop.
Promise patterns: http://taoofcode.net/promise-anti-patterns/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// bad | |
function anAsyncCall() { | |
var promise = doSomethingAsync(); | |
promise.then(function() { | |
somethingComplicated(); | |
}); | |
return promise; | |
} | |
// good | |
function anAsyncCall() { | |
var promise = doSomethingAsync(); | |
return promise.then(function() { | |
somethingComplicated() | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// bad | |
function workMyCollection(arr) { | |
var resultArr = []; | |
function _recursive(idx) { | |
if (idx >= resultArr.length) return resultArr; | |
return doSomethingAsync(arr[idx]).then(function(res) { | |
resultArr.push(res); | |
return _recursive(idx + 1); | |
}); | |
} | |
return _recursive(0); | |
} | |
// good | |
// Unlike the recursive non-solution, this will kick off all the async calls in parallel | |
function workMyCollection(arr) { | |
return q.all(arr.map(function(item) { | |
return doSomethingAsync(item); | |
})); | |
} | |
// good | |
// If you need to run your promises in series, you can use reduce. | |
function workMyCollection(arr) { | |
return arr.reduce(function(promise, item) { | |
return promise.then(function(result) { | |
return doSomethingAsyncWithResult(item, result); | |
}); | |
}, q()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// bad | |
var deferred = Q.defer(); | |
doSomethingAsync().then(function(res) { | |
res = manipulateMeInSomeWay(res); | |
deferred.resolve(res); | |
}, function(err) { | |
deferred.reject(err); | |
}); | |
return deferred.promise; | |
// good | |
return doSomethingAsync().then(function(res) { | |
return manipulateMeInSomeWay(res); | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// bad | |
var promise; | |
if (asyncCallNeeded) | |
promise = doSomethingAsync(); | |
else | |
promise = Q.resolve(42); | |
promise.then(function() { | |
doSomethingCool(); | |
}); | |
// good |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bad | |
loadSomething().then(function(something) { | |
loadAnotherthing().then(function(another) { | |
DoSomethingOnThem(something, another); | |
}); | |
}); | |
// Good | |
q.all([loadSomething(), loadAnotherThing()]) | |
.spread(function(something, another) { | |
DoSomethingOnThem(something, another); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// bad | |
somethingAsync.then( | |
function() { | |
return somethingElseAsync(); | |
}, | |
function(err) { | |
handleMyError(err); | |
}); | |
// good | |
somethingAsync | |
.then(function() { | |
return somethingElseAsync(); | |
}) | |
.then(null, | |
function(err) { | |
handleMyError(err); | |
}); | |
// or | |
somethingAsync | |
.then(function() { | |
return somethingElseAsync(); | |
}) | |
.catch(function(err) { | |
handleMyError(err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment