Processing Status in Asynchronous Node Classes

I am relatively new to Node, and one thing that concerns me is how I design classes that need to maintain a state where I cannot be sure that they will not be called again before they finish processing the first call.

eg.

var thing = new Thing();

// Doesn't maintain a state, call it as much as you like
thing.doSomething(function() { alert('done') });
thing.doSomething(function() { alert('done') });
thing.doSomething(function() { alert('done') });

// Maintains some sort of internal state
thing.doSomethingStateful(function() { alert('done') });

// Calling it again messes up the first call if the callback
// hasn't been hit yet because it still waiting on an I/O 
// operation to complete. 
thing.doSomethingStateful(function() { alert('done') });

Is there a design template that helps solve this problem? Should I always strive to make Node classes standstill and use an internal state object to track the state of each call? Or is there a convention to differentiate classes that can be used asynchronously and those that support state?

+3
source share
2 answers

, node.js.

, single, . , .

, -. , . , - , .

. . , .

, . doSomethingStateful, , , .

, - , Async.js .

+2

, MBlanc, , , .

, (, ​​ ) , . - ( , ).

+1

All Articles