task.js is an
experimental library for ES6 that makes
sequential, blocking I/O simple and beautiful, using the power of
JavaScript’s new yield operator.
Tasks are interleaved like threads, but they are cooperative
rather than pre-emptive: they block on
promises with
yield. Here’s an example
using jQuery:
spawn(function*() {
var data = yield $.ajax(url);
$('#result').html(data);
var status = $('#status').html('Download complete.');
yield status.fadeIn().promise();
yield sleep(2000);
status.fadeOut();
});
task.js works with any framework or library that uses the Promises/A spec.
The power of task.js comes from ES6 generators, which are currently only available in Firefox. You can try it out in Firefox, although there are a few remaining incompatibilities with the current ES6 spec (which is still a work in progress).
Here’s a “hello world” of tasks that will work in Firefox:
<script type="application/javascript" src="task.js"></script>
<!-- 'yield' and 'let' keywords require version opt-in -->
<script type="application/javascript;version=1.8">
function hello() {
let { spawn, sleep } = task;
spawn(function() { // Firefox does not yet use the function* syntax
alert("Hello...");
yield sleep(1000);
alert("...world!");
});
}
</script>
The spawn function takes a generator function and starts
running it as a concurrently-executed task. Every time the task yields
a promise, the scheduler blocks the task until the promise is
fulfilled.
Creates a new task and automatically starts it.
A function to produce the generator for running the task. The function
is called with this bound to the new task.
A concurrent and suspendable computation.
A function to produce the generator for running the task. The function
is called with this bound to the new task.
Returns the currently executing task, if any.
Starts the task. If the task is blocked on a promise, the task will not resume executing until the promise is fulfilled. Otherwise, the scheduler will resume the task at some point.
Pauses the task. Even when the task is ready to resume, it will not resume until it is started again. If the task is waiting on a promise that becomes fulfilled while the task is paused, its result will not be lost; it will be returned whenever the task is started again.
A task cannot be paused if it is currently running. To pause a running task after it reaches its next yield-point, enqueue a callback to pause it in a different turn of the event loop:
enqueue(function() {
task.pause();
});
Indicates whether the task is started.
Indicates whether the task is currently executing.
get, put, call, addCallback, addErrback, addBoth, addCallbacks, timeout
An object encapsulating a suspendable computation. In ES6, generators can be conveniently created using the yield operator.
Resumes the computation to its next yield point or completion, whichever comes first. The computation should yield either a promise, indicating that the computation is blocked until the promise is fulfilled, or a falsey value, indicating that the computation is immediately ready to resume.
The result to resume the previous yield with.
Resumes the computation by throwing an error. The computation runs to its next yield point or completion, whichever comes first. The computation should yield either a promise, indicating that the computation is blocked until the promise is fulfilled, or a falsey value, indicating that the computation is immediately ready to resume.
The error to throw in the resumed computation.
Cancels the computation, executing any live finally
blocks remaining in the generator. Closing the computation should not
expect to be able to return any more promises; they will be ignored by
the scheduler, which will consider the computation to be terminated.
ES6 allows generator functions to return a value, but the Firefox JavaScript engine (the only that currently implements generators) does not yet allow this. To produce a result value from a task, throw an instance of this class.
The task result value.
An object representing an “eventual value,” i.e., a value that may not be ready yet but will be provided at some point in the future.
Registers callbacks to be called by the promise.
Callback to be called by the promise when it produces its result.
result : A The result value produced by the promise.
Callback to be called by the promise when it throws an error while trying to produce its result.
error : any The error thrown while attempting to produce a result.
Callback to be called by the promise while making progress towards producing its result.
Cancel the retrieval of the promise’s value. If the promise is already fulfilled, this operation does nothing.
Converts an ordinary value into a fulfilled promise.
The promise’s resolved value.
Produces a promise that is never fulfilled.
Produces a promise that is resolved when all the given promises are resolved. The resolved value is an array of each of the resolved values of the given promises.
If any of the promises is rejected, the joined promise is rejected with the same error, and any remaining unfulfilled promises are cancelled.
A promise to join.
Produces a promise that is fulfilled when any one of the given promises is fulfilled. As soon as one of the promises is fulfilled, whether by being resolved or rejected, all the other promises are cancelled.
A promise to choose from.
Produces a promise that is resolved when a given amount of time
elapses, using the builtin global setTimeout function.
The promise’s resolved value is the actual number of milliseconds elapsed since the promise was created.
The number of milliseconds to sleep for.
Indicates whether to compensate for imprecision in the host platform by continuing to sleep if the timeout completes before the indicated number of milliseconds have elapsed.
An object providing a scheduling policy. A scheduler maintains an internal collection of scheduled tasks and decides which task to choose next for execution.
Chooses a scheduled task for the runtime to execute next, removing it from the internal collection of scheduled tasks.
Adds a task to the scheduler’s internal collection of scheduled tasks. The task is guaranteed not to be currently scheduled.
The task to schedule.
Removes a task from the scheduler’s internal collection of scheduled tasks. The task may or may not already be scheduled.
The task to unschedule.
Returns the currently-installed system scheduler.
Installs a new system scheduler. Any existing tasks will continue to be scheduled via the scheduler they were created with, but new tasks will be associated with the new scheduler.
The new scheduler.
A scheduler that chooses tasks randomly using Math.random.
A low-level promise that can be manually fulfilled.
get, put, call, addCallback, addErrback, addBoth, addCallbacks, timeout
A partial implementation of a promise, providing utility methods
common in popular promise implementations. To implement the full
promise API, instances must be given then
and cancel methods.