Skip to content

Commit

Permalink
edited flatten-javascript-pyramids-with-async-js.md
Browse files Browse the repository at this point in the history
  • Loading branch information
n1k0 committed Feb 6, 2013
1 parent 11855b6 commit a5f8e0e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
26 changes: 23 additions & 3 deletions pages/code/2013/flatten-javascript-pyramids-with-async-js.md
Expand Up @@ -37,7 +37,7 @@ Classic. But what if you want to perform multiple insertions, eg. to load a bunc
// … we could add many more
];

it ("should do something useful with moods", function(done) {
it("should do something useful with moods", function(done) {
store(moods[0], function(err, mood) {
assert.ifError(err);
store(moods[1], function(err, mood) {
Expand Down Expand Up @@ -71,7 +71,7 @@ Here we go again, [callback hell] and unmanageable pyramids.
// … we could add many more
];

it ("should do something useful with moods", function(done) {
it("should do something useful with moods", function(done) {
async.parallel([
function(cb) {
store(mood[0], function(err, mood) {
Expand Down Expand Up @@ -125,7 +125,7 @@ Indeed, this is definitely not [DRY code]. But one has to be creative to turn a
// … we could add many more
];

it ("should do something useful with moods", function(done) {
it("should do something useful with moods", function(done) {
load(moods, function(err, storedMoods) {
assert.ifError(err);
// now let's test stuff with stored moods
Expand All @@ -134,6 +134,26 @@ Indeed, this is definitely not [DRY code]. But one has to be creative to turn a
});
});

**Edit:** there's even a built-in `async.map()` function, not sure how I missed it; so the code is even shorter:

describe("moods tests", function() {
var moods = [
"2013-02-01:n1k0:sunny"
, "2013-02-02:n1k0:cloudy"
, "2013-02-03:n1k0:stormy"
, "2013-02-04:n1k0:rainy"
// … we could add many more
];

it("should do something useful with moods", function(done) {
async.map(moods, store, function(err, storedMoods) {
assert.ifError(err);
// now let's test stuff with stored moods
done();
});
});
});

[Async.js] is a great package and one of the most popular of the [node ecosystem], but there are [many others](https://npmjs.org/browse/keyword/async).

Such a library combined with a [functional approach](http://cjohansen.no/talks/2012/sdc-functional/) provides a killer combo to solve your daily problems when programming [Javascript] (<small>with a small *s* because [I'm punk](https://twitter.com/domenic/status/296021285736161280).</small>)
Expand Down
2 changes: 1 addition & 1 deletion static/packed.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a5f8e0e

Please sign in to comment.