-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mention async.js #8
Comments
first and foremost I want to cover simple and 'pure js' solutions to common problems without resorting to third party libs. however I also use async in certain cases and see the value in libraries that let you run a callback on a group of callbacks jquery deferreds do it like this (syntax from memory):
async does it like this:
minimal libs like queue do it like this:
at a high level these are the same pattern. async has lots of options and flavors, queue is very minimal at 554 bytes, jquery deferreds are syntax heavy and complex but also pervasive maybe i could distill these thoughts into a description of the 'callback for many callbacks' problem somehow? |
heres a simplified version of the question: say you want to download two cat photos and combine them into one cat photo:
whats the simplest way to get both kittens in one callback? |
naive implementation:
obviously there are problems with this but the goal is to show when you need third party libs and when you dont. is this the simplest way to show the complexity of the problem? |
For me, the other case of callback hell, besides "wait till this set of async things finish" and nested callbacks is code that has conditionals. Suppose that there are some steps, some of which need to happen async, but some that can be skipped if a value is available already. function step3(value) {
//Do step 3
}
function step2(value) {
//Do step 2
step3(value);
}
function step1() {
//Do step 1
step2(value);
}
if (alreadyHaveValue()) {
step2(existingValue);
} else {
step1();
}
} For me, that is when something like promises or some library with a .then() which allows processing plain values or deferred promises, comes in useful. Using q: Q.fcall(function () {
if (alreadyHaveValue()) {
//Existing value is not a promise
return existingValue;
} else {
return step1ReturningPromiseOrJustInlineHere();
}
}).then(function (value) {
//Do step 2, could have conditionals
}).then(function (value) {
//Do step 3, could have conditionals
}); It gives a more linear read of the logic flow. No need for inside out, functions with names that are jumped to for the next step. That is too reminiscent of goto. |
I hate plugging my own solutions, but hey, you asked on Twitter. Maybe you can reduce this problem into three basic functions, series(), parallel() and limited()? http://book.mixu.net/ch7.html Would love a link back if you do use it, though these days I'd write a lot of that stuff differently, works though. |
I think async.js certainly worth some words. And the link to @mixu's book is now http://book.mixu.net/node/ch7.html. |
Good stuff! I think it's worth also mentioning async.js (https://github.com/caolan/async/). I use it a lot to keep async stuff in check, especially parallel().
The text was updated successfully, but these errors were encountered: