It's an interesting presentation, but it neglects an obvious solution to the nested callbacks problem, which are the various "async"-like control-flow libraries out there. More importantly, it neglects a critical problem with all of these approaches, which is being able to observe asynchronous state (from a debugger, repl, or whatever) when things go wrong -- when a callback (or event) you expected doesn't fire, or t…
Sane Async Patterns in JavaScript
21–30 of 30 posts
Re: Sane Async Patterns in JavaScript
#22My JS programming often looks something like: https://gist.github.com/anonymous/5314313
I don't really care if the APIs I use have callbacks or promises, both fit in just fine.
Re: Sane Async Patterns in JavaScript
#23Have you tried state machines? My JS programming often looks something like: https://gist.github.com/anonymous/5314313 I don't really care if the APIs I use have callbacks or promises, both fit in just fine.
Re: Sane Async Patterns in JavaScript
#24While looking through the slides, I was wondering: Is there some community-standard way of naming/declaring a JS function to let the programmer know just by reading the function's name that it will return a promise instead of a final computed value? I'm interested in knowing how people handle this. It's easy to forget that a function doesn't return a promise if the function name makes no mention of it, so I just make…
myAsyncFunc = function (param1, param2, callback) {
var usingPromises = (callback === undefined);
var returnVal = usingPromises ? Q.defer() : this;
someFunction(function (err, res) {
if (usingPromises) {
err ? returnVal.reject(err) : returnVal.resolve(res);
} else {
callback(err, res);
}
});
return returnVal;
};
So if I call myAsyncFunc(foo, bar) it returns a promise and if I call myAsyncFunc(foo, bar, cb) it returns itself for chaining.Re: Sane Async Patterns in JavaScript
#25"Nested spaghetti" strikes me as a mixed metaphor. I'd have to ask an expert on Italian cuisine, but I don't think spaghetti nests. Literary aspects aside, I don't think the "pyramid of doom" is what is meant by spaghetti. In the pyramid, it's easy to see the sequence in which things happen. In spaghetti code, it's hard to see the sequence -- that's the problem. The fact that the pyramid makes it easy to see the sequ…
Re: Sane Async Patterns in JavaScript
#26"Nested spaghetti" strikes me as a mixed metaphor. I'd have to ask an expert on Italian cuisine, but I don't think spaghetti nests. Literary aspects aside, I don't think the "pyramid of doom" is what is meant by spaghetti. In the pyramid, it's easy to see the sequence in which things happen. In spaghetti code, it's hard to see the sequence -- that's the problem. The fact that the pyramid makes it easy to see the sequ…
Re: Sane Async Patterns in JavaScript
#27Re: Sane Async Patterns in JavaScript
#28Put another way, the obviously missing pattern to me is to just move the concurrency up to the level of having multiple threads, and then use synchronous interfaces for synchronous operations. Now that Web Workers are starting to become more widely supported, I don't understand why we aren't heading for synchronous APIs as fast as humanly possible.
Re: Sane Async Patterns in JavaScript
#29Have you tried state machines? My JS programming often looks something like: https://gist.github.com/anonymous/5314313 I don't really care if the APIs I use have callbacks or promises, both fit in just fine.