JSScheduleAPI: Difference between revisions
Jump to navigation
Jump to search
(+ Details) |
|||
Line 8: | Line 8: | ||
var my_schedule = Schedule.queue(f); | var my_schedule = Schedule.queue(f); | ||
To synchronize upon some | To synchronize upon some barrier "foo": | ||
var my_schedule = Schedule.on("foo"); | var my_schedule = Schedule.on("foo"); | ||
// Barrier "foo" is created if it did not exist yet. | |||
Both methods return objects of class <tt>Schedule</tt>. | Both methods return objects of class <tt>Schedule</tt>. See the methods of <tt>Schedule</tt> for more on how to wait on a barrier or trigger it. | ||
=== Constructor <tt>Schedule</tt> === | === Constructor <tt>Schedule</tt> === | ||
Line 21: | Line 22: | ||
To schedule asynchronous execution of a function <tt>g</tt> in case of successful execution: | To schedule asynchronous execution of a function <tt>g</tt> in case of successful execution: | ||
var my_schedule2 = my_schedule.queue(g); // |g| receives result | var my_schedule2 = my_schedule.queue(g); | ||
// |g| receives result | |||
// of |my_schedule| | |||
To execute a function <tt>g</tt> in case of error: | To execute a function <tt>g</tt> in case of error: | ||
var my_schedule2 = my_schedule.trap(g); | var my_schedule2 = my_schedule.trap(g); | ||
// |g| receives error | |||
// of |my_schedule| | |||
To execute a function <tt>g</tt> regardless of success or error: | To execute a function <tt>g</tt> regardless of success or error: | ||
Line 44: | Line 49: | ||
or | or | ||
var my_schedule2 = my_schedule.queue(f, {delay: 100}); | var my_schedule2 = my_schedule.queue(f, {delay: 100}); | ||
In both cases, <tt>delay</tt> is counted in milliseconds after the task is dequeued. |
Revision as of 17:26, 22 March 2012
Object Schedule
Object Schedule offers a mechanism for:
- launching tasks asynchronously;
- setting up synchronization barriers between asynchronous tasks.
To schedule the asynchronous execution of a function f:
var my_schedule = Schedule.queue(f);
To synchronize upon some barrier "foo":
var my_schedule = Schedule.on("foo"); // Barrier "foo" is created if it did not exist yet.
Both methods return objects of class Schedule. See the methods of Schedule for more on how to wait on a barrier or trigger it.
Constructor Schedule
This class offers mechanisms for:
- waiting for successful execution of an asynchronous task;
- trapping errors in asynchronous tasks (inherited from Promise):
- signaling success/error in a schedule (inherited from Promise).
To schedule asynchronous execution of a function g in case of successful execution:
var my_schedule2 = my_schedule.queue(g); // |g| receives result // of |my_schedule|
To execute a function g in case of error:
var my_schedule2 = my_schedule.trap(g); // |g| receives error // of |my_schedule|
To execute a function g regardless of success or error:
var my_schedule2 = my_schedule.always(g);// |g| receives nothing
To create an empty Schedule, for synchronization purposes:
var my_schedule2 = new Schedule();
To signal a success:
var my_schedule2.resolve(result);
To signal an error:
var my_schedule2.reject(error)
Further options
To delay execution by 100 milliseconds:
var my_schedule = Schedule.queue(f, {delay: 100});
or
var my_schedule2 = my_schedule.queue(f, {delay: 100});
In both cases, delay is counted in milliseconds after the task is dequeued.