JSScheduleAPI

From MozillaWiki
Jump to: navigation, 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 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 approximately 100 milliseconds (from now):

  var my_schedule = Schedule.queue(f, {delay: 100});

or to delay execution until approximately 100 milliseconds after the end of my_schedule2

  var my_schedule2 = my_schedule.queue(f, {delay: 100});