JSScheduleAPI

From MozillaWiki
Revision as of 15:53, 22 March 2012 by Yoric (talk | contribs)
Jump to navigation Jump to search

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 global barrier "foo":

 var my_schedule = Schedule.on("foo");

Both methods return objects of class Schedule.

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.queueg; // |g| receives result

To execute a function g in case of error:

  var my_schedule2 = my_schedule.trap(g);  // |g| receives error

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});