This is just a quick reply, so I haven't refactored the `timeyWimeyBall` 
method, but I would suggest that suspend 
(https://github.com/jmar777/suspend) could be useful here (requires node 
v0.11.3+ or gnode for earlier versions).

Here's how you could accomplish the parallel coordination:

suspend.run(function*() {
    timeyWimeyBall('/offers/point-a', 'Offer A initial body', 
suspend.fork());
    timeyWimeyBall('/offers/point-b', 'Offer B initial body', 
suspend.fork());

    var answerers = yield suspend.join();

    answerers[0]('Answer A initial body', suspend.fork());
    answerers[1]('Answer A initial body', suspend.fork());
    yield suspend.join();
});

On Wednesday, January 8, 2014 4:54:08 AM UTC-5, Stuart P. Bentley wrote:
>
> This is an excerpt from the test suite I'm developing for a REST signaling 
> component in my app. It creates two parallel connection offers, and then 
> checks that no wires get crossed when each offer gets answered.
>
> To ensure that both offers are listening for answers when I send the first 
> answer, I'm awaiting the listen phase for both offers (using 
> mbostock/queue, aka 'queue-async' in npm) before I send the answers. The 
> async code path that the response to the answer goes down, which gets 
> initiated before the answers are posted, has to resolve *after* the 
> resolution of the awaited listening phase. I don't even know how I'd write 
> this with promises.
>
>       function timeyWimeyBall(endpoint, obody, cb) {
>         localPost(endpoint, obody, function(err, res, body) {
>           // This will be set a couple callbacks down
>           var expectedAnswer, finalCallback;
>           
>           localGet(res.headers.location, receiveAnswer);
>           
>           cb(err,function answerer(abody, fcb) {
>             expectedAnswer = abody; finalCallback = fcb;
>             localGet(endpoint,function(err,res,body){
>               localPost(res.headers.location, abody);
>             });
>           });
>           
>           function receiveAnswer(err, res, body) {
>             assert(body == expectedAnswer);
>             finalCallback && finalCallback(err);
>           }
>         });
>       }
>
>       queue()
>         .defer(timeyWimeyBall, '/offers/point-a', 'Offer A initial body')
>         .defer(timeyWimeyBall, '/offers/point-b', 'Offer B initial body')
>         .await(function(err, answererA, answererB){
>           queue()
>             .defer(answererA,'Answer A initial body')
>             .defer(answererB,'Answer B initial body')
>             .await(done);
>         });
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to