Edmond's approach is a good one. If you want something that will handle
some of the bookkeeping for you, check out Mozilla's TaskJS (
http://taskjs.org) – not to be confused with Bert Belder's 'tasks'.

A few months ago, there was some discussion on es-discuss about making
cancellation a part of the DOM Promises spec, which got shelved because
there was no consensus about how to deal with some of the consequences.
Kris Kowal (of Q fame) summarized some of the issues at
https://github.com/kriskowal/q/issues/64.

Also, while you can use EventEmitters for control flow, trying to use them
for this is probably just going to result in confusion for everybody.
You're better off handing around explicit cancel() and isCanceled()
functions (or values).

I bring all this up not to be pedantic, but to point out that in general
it's hard to handle aborting of asynchronous continuation chains safely.
Async execution is like a shark – it often has no choice but to
keep moving forwards. Doing it safely is likely to remain fairly hands-on
and verbose, because it's going to require a bunch of code that's aware of
how your async code forks and joins.

F

On Sunday, December 8, 2013, Dave Horton 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 
> [email protected]<javascript:_e({}, 'cvml', '[email protected]');>
> To unsubscribe from this group, send email to
> [email protected] <javascript:_e({}, 'cvml',
> 'nodejs%[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] <javascript:_e({}, 'cvml',
> 'nodejs%[email protected]');>.
> 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 [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