JSScheduleAPI

From MozillaWiki
Revision as of 15:50, 22 March 2012 by Yoric (talk | contribs) (Created page with "Module Schedule is based on module Promise, which landed about 5 months ago. === Object |Schedule| === Object |Schedule| offers a mechanism for: * launching tasks asynchronousl...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Module Schedule is based on module Promise, which landed about 5 months ago.

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.queue(g); // |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 a number of milliseconds:

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

or

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