Name

Description

SK.Sync(
   functions,
   callback_ok,
   callback_failure
)

Constructor

This library is used to synchronize a sequential execution of functions which normally are executed without any synchronization. 

For example all SK.API.execute calls are running asynchronously by default. If you want to execute 10 API calls one after another and make sure they are executed in order and waiting for each other without making the code heavy with IFs and function callbacks — that's the library that would help you do it.

The "functions" is an array of functions. Each one has to have the following interface:
   function( sync_obj ) 

The "sync_obj" is the same "SK.Sync" object instance. The function has to invoke 
   sync_obj.ready( )
when the function finishes its operation. This will trigger the execution of the next function. If you want to interrupt the execution of all functions you should invoke
   sync_obj.failure( "...your message here..." )
This will invoke your
   callback_failure( "...your message here..." )

After all functions are executed the callback_ok will be invoked without parameters.

run( )

This method fires up the execution of the first function in the list.


 

Examples
Synchronizing a few API calls.

<script type="text/javascript">
   function getDatabases( sync_obj ) {
      new SK.API(
         null // Will use the current session ID
      ).execute(
         'db.get_all',
         {},
         function( success, message, data ) {
            // Dump the list
            if ( success ) {
               data.databases.each(function(d){
                  $('log').grab( new Element( 'div', { text: d.name } ) );
               });
               // Tell the SK.Sync library the execution of this function has finished
               this.sync_obj.ready( );
            } else {
               this.sync_obj.failure( message );
            }
         }.bind( { sync_obj: sync_obj } )
      );
   }

   function pause( sync_obj ) {
      $('log').grab(new Element( 'hr' ));
      sync_obj.ready( ); // Tell the library we're done
   }

   function testDone( ) {
      $('log').grab(new Element( 'div', { text: 'Done!' } ) );
   }

   function testFailure( message ) {
      $('log').grab(new Element( 'div', { text: 'FAILURE: ' + message } ) );
   }

   var sync_obj = new SK.Sync(
      [
         getDatabases,
         pause,
         getDatabases,
         pause,
         getDatabases
      ],
      testDone,
      testFailure
   );

   sync_obj.run( );
</script>
<div id="log"></div>