apps will define their own abstractions for the asynchrony. In a few cases (chat servers), it may be appropriate. In most cases, we can abstract away callbacks from application logic through abstraction.
whose addresses are passed in readfds, writefds, and errorfds to see if some of their descriptors are ready for reading, ... <snip> PRIMITIVE STATUS. Scheduler Initial Program Primitive Status Primitive Value Error Condition Application Callback
Thread(function() { var data = fs.readFile("/etc/sudoers"); console.log(data); }); sleep(); THREADS. Scheduler Initial Program Primitive Status Primitive Value Error Condition Application Callback
Thread(function() { var data = fs.readFile("/etc/sudoers"); console.log(data); }); sleep(); THREADS. Scheduler Initial Program Primitive Status Primitive Value Error Condition Application Callback The scheduler does essentially the same thing when using threads.
Thread(function() { var data = fs.readFile("/etc/sudoers"); console.log(data); }); sleep(); THREADS. Scheduler Initial Program Primitive Status Primitive Value Error Condition Application Callback Same with initial program.
frame stack frame local variable values + current position The main difference with threads is that the callback structure is more complicated. callback resume thread
Thread(function() { var data = fs.readFile("/etc/sudoers"); console.log(data); }); sleep(); THREADS. Scheduler Initial Program Primitive Status Primitive Value Error Condition Application Callback When data is ready, resume the associated thread.
a GIL ▪ Unexpected interleaved code and context switching overhead ▪ Can eliminate if desired by disabling pre- emptive scheduling ▪ More memory required for callback structure DIFFERENCES. The thread abstraction, which is useful to manage asynchronous events with deterministic order, has varying implementation-defined characteristics. It will always require more memory.
Fiber(function() { var data = fs.readFile("/etc/sudoers"); console.log(data); }); sleep(); FIBERS. Scheduler Initial Program Primitive Status Primitive Value Error Condition Application Callback Same with initial program.
data) { fiber.resume(data); }); return Fiber.yield(); }; IMPLEMENTATION. Scheduler Initial Program Primitive Status Primitive Value Error Condition Application Callback Fibers implement the status and value parts of the scheduler in the language, but fundamentally have the same data structures as threads.
frame stack frame local variable values + current position callback resume thread The fact that fibers are "lighter" is an implementation detail. Having to implement manual yielding is a pain.
to flot() ▪ Resiliance to Errors: All callers needs to remember to call flot() ▪ Composability: The framework can no longer simply ask for a view and render it as needed PROBLEMS.
var element = $(template(json)).appendTo('body'); yield requestAnimationFrame(); element.fadeIn(); }; var scheduler = new Scheduler; scheduler.schedule(task); YIELDING.
var element = $(template(json)).appendTo('body'); yield requestAnimationFrame(); element.fadeIn(); }; var scheduler = new Scheduler; scheduler.schedule(task); YIELDING. Scheduler Initial Program Primitive Status Primitive Value Error Condition Application Callback Here, the task is yielding an object that knows how to be resumed.
value to built-in primitives that the VM knows how to figure out. In order for generators to be useful, we will need to expose those concepts to userland.
function(err, file) { if (err) { promise.error(err); } else { promise.resolve(file.read()); } }); return promise; } PROMISES. Scheduler Initial Program Primitive Status Primitive Value Error Condition Application Callback Here, we are still allowing the VM to let us know when the file is ready, but we control the callback manually.
$("input#confirm") .show() .one('keypress', function(e) { if (e.which === 13) { promise.resolve(this.value); } }); return promise(); }; spawn(function*() { var entry = yield prompt(); console.log(entry); }); BETTER PROMPT. Scheduler Initial Program Primitive Status Primitive Value Error Condition Application Callback In this case, we are in control of both the status information and the value of the status.
status/value that a scheduler can use. In JavaScript, generators + promises give us a way to use a sequential abstraction for asynchronous events with deterministic order. In a UI app, a small part of the total interface may have deterministic order, and we can use this abstraction on demand. In general, I would prefer to use promises together with a higher level sequential code abstraction than use promises directly in application code.
promise.resolve(string) end Thread.yield promise end end BETTER PRIMITIVES. In languages that already have threads, promises can be used to provide more power in the existing scheduler. This can be implemented in terms of sync primitives.
application or application framework. They are typically handled as asynchronous events. If they have deterministic order, the above techniques may work.
{ return [this.get('firstName'), this.get('lastName')].join(' '); }.property('firstName', 'lastName') }); $("#person").html("<p>" + person.get('fullName') + "</p>"); person.addObserver('fullName', function() { $("#person p").html(person.get('fullName')); }); DECLARATIVE. The .property is the way we describe that changes to firstName and lastName affect a single output.
the scenes, Ember defers the propagation of the changes until the turn of the browser's event loop. Because we know the dependencies of fullName, we can also coalesce the changes to firstName and lastName and only trigger the fullName observer once.
for data flow for objects that implement observability. For external objects and events, you start with asynchronous observers (either out from the binding system or in from the outside world).
{ return [this.get('firstName'), this.get('lastName')].join(' '); }.property('firstName', 'lastName') }); var personView = Ember.Object.create({ person: person, fullNameBinding: 'person.fullName', template: compile("<p>{{fullName}}</p>") }); person.append(); $.getJSON("/person/me", function(json) { person.set('firstName', json.firstName); person.set('lastName', json.lastName); }); EXTENDED REACH. For common boundary cases, like DOM, we can wrap the external objects in an API that understands data-binding. This means that we won't need to write async code to deal with that boundary.
external system, make sure that the abstraction has the same semantics when dealing with the outside world. In Ember's case, the same coalescing guarantees apply to DOM bindings.
have large amounts of async code. In many cases, an application will want to expose abstractions for its own async concerns that allow the bulk of the application code to proceed without worrying about it. One common pattern is using an async reactor for open connections but threads for the actual work performed for a particular open connection.
a strict deterministic order can make use of different abstractions that async code that arrives in non-deterministic order. Internal events can make use of different abstractions than external events.