From abce48e69df54388a97fd2202c26ef23082c9f6f Mon Sep 17 00:00:00 2001 From: Geoff Wagstaff Date: Thu, 15 Mar 2012 18:39:54 +0000 Subject: [PATCH 1/2] Adapter docs for convention-breakers --- README.markdown | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.markdown b/README.markdown index d6d95e9..7ab3132 100644 --- a/README.markdown +++ b/README.markdown @@ -69,3 +69,25 @@ Also you can use group with a dynamic number of common tasks. ); *Note* that we both call `this.group()` and `group()`. The first reserves a slot in the parameters of the next step, then calling `group()` generates the individual callbacks and increments the internal counter. + +If the library you're using for asynchronous calls doesn't follow node's convention of function callback(err, res), then you could consider using an adapter like the following: + + Step( + // Two actions in parallel + function loadStuff() { + naughtyLibrary.doSomething(foo, (function(){ + var x = this.parallel(); + return function(rr){ + err = rr.status != 'success' ? rr : null; + x(err, rr); + } + }.bind(this))()); + fs.readFile("/etc/passwd", this.parallel()); + }, + // Show the result when done + function showStuff(err, libRes, users) { + if (err) throw err; + console.log(libRes); + console.log(users); + } + ) \ No newline at end of file From 635c5d15ff769a6e3757c06c39605fed55ceb26c Mon Sep 17 00:00:00 2001 From: Geoff Wagstaff Date: Thu, 15 Mar 2012 20:05:36 +0000 Subject: [PATCH 2/2] Make example clearer --- README.markdown | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/README.markdown b/README.markdown index 7ab3132..0c76185 100644 --- a/README.markdown +++ b/README.markdown @@ -72,16 +72,18 @@ Also you can use group with a dynamic number of common tasks. If the library you're using for asynchronous calls doesn't follow node's convention of function callback(err, res), then you could consider using an adapter like the following: + function adapter(){ + var x = this.parallel(); + return function(rr){ + err = rr.status != 'success' ? rr : null; // Or whatever logic is necessary to ascertain status + x(err, rr); + } + } + Step( // Two actions in parallel function loadStuff() { - naughtyLibrary.doSomething(foo, (function(){ - var x = this.parallel(); - return function(rr){ - err = rr.status != 'success' ? rr : null; - x(err, rr); - } - }.bind(this))()); + naughtyLibrary.doSomething(foo, adapter.call(this)); fs.readFile("/etc/passwd", this.parallel()); }, // Show the result when done @@ -90,4 +92,5 @@ If the library you're using for asynchronous calls doesn't follow node's convent console.log(libRes); console.log(users); } - ) \ No newline at end of file + ) +