You could use async.mapSeries (https://github.com/caolan/async) with an array of functions and the iterator would make the check to execute the function, e.g.:
var async = require('async'); var cancelled = false; var cnt = 1; async.mapSeries([ function step1() { console.log('step 1'); }, function step2() { console.log('step 2'); cancelled = true; }, function step3() { console.log('step 3'); }], // iterator function iter(fn, cb) { // check if the function should run if (cancelled) { cb(null, 'skipped '+cnt++); } else { fn(); cb(null, 'step '+cnt++); } }, // result function(err, results) { // check for error if (err) { // do something console.log(err); } // process results, if any if (results) { console.log(results); } } ); -Edmond On Dec 8, 2013, at 3:12 PM, Dave Horton <d...@dchorton.com> wrote: > I'm trying to figure the cleanest way to deal with a scenario where you have > a long chain of callbacks that are executing, and then something in your > environment/program changes in such a way that negates the need for that > work, and could even lead to invalid results if it is allowed to progress. > > An example would probably help. Say I have an app that takes a connection > from client, and then starts some work on their behalf. The work entails > marshalling the resources of a bunch of network resources on their behalf, so > that work gets kicked off. Then the client disconnects or otherwise signals > he wants to cancel the original request. It might look something like this: > > var app = require('my-app-thingy')() ; > app.onRequest( function( req, res ){ > //1: we just got a request from a client > > writeTransactionStartTimeToDatabase() ; //write a record to database > to record start time > > req.on('canceled', function() { > //3: we just got notified that the client has canceled the > request > writeTransactionEndTimeToDatabase() > }) ; > > //2: start some long running activity > acquireNetworkResource( function(err, resource) { > > //4: we got the requested resource > > doSomeOtherAsynchronousActivity( function( err ) { > //5: all done > writeTransactionEndTimeToDatabase() ; //just wrote > wrong time to database > }) ; > }) ; > }) > > At time 1, I get a request from a client, and immediately (at time 2) launch > some work on their behalf. At time 3, however, the request is canceled. Now > I need to shut down that work. At time 4, part of the long running task > returns, and since I am not checking the status of the request I launch > another activity -- at time 5, that returns and I now write a second record > to the database recording the incorrect end time -- the proper end time was > written earlier (time 2) and is now overwritten. > > Obviously, I could just check the status of the request at every callback > stage, but that quickly gets ugly (what if there are multiple conditions that > needed to be checked). > > Is there a pattern for writing code to handle this type of scenario simply > and cleanly? > > -- > -- > 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 nodejs@googlegroups.com > To unsubscribe from this group, send email to > nodejs+unsubscr...@googlegroups.com > 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 nodejs+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. -- -- 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com 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 nodejs+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.