GithubHelp home page GithubHelp logo

synchronizer-js's Introduction

synchronizer-js v1.0.0

Synchronizer.js is a tiny (666 bytes minified) utility class that you can use to synchronize asynchronous operations, like AJAX callbacks for example. It wraps the usual operation coutner in a small class so you can run more than one at once.

It's 90% inspired by this article : http://forrst.com/posts/callWhenDone_a_simple_synchronized_callback_clo-zi1

Usage

Don't forget to reference Synchronizer.js or Synchronizer.min.js in your html.

// initalize the object with the callback method that will be called when all operations
// are completed
var synchronizer = new Synchronizer(function(){ /* ... */ });

// make a call to add() everytime that you add a new operation
synchronizer.add();
myOperation(myOperationCallback);

// in the callback of your operation, make a call to done() to register the end of the operation
function myOperationCallback() {
	synchronizer.done();
	/* ... */
}

Cases you should avoid:

  • If you call less done() than add(), the callback will never be called.
  • If you call less add() than done(), an exception will be thrown.

"Advanced" usage

You can pass an int value to add() and done() to change the number of operations that were completed

var synchronizer = new Synchronizer(function(){ /* ... */ });

// you know that there are 5 operations that will have to be completed
synchronizer.add(5);

// ... in one callback, for some obscure reason if you need to complete the 5 operations instead of one
synchronizer.done(5);

Demo

A small demo page is available

See also

If this does not enough for you, there are plenty of libraries that does a lot more. Check for example async.

Building the JS

You can also build the javascript file yourself (that step is not required tho).

The javascript is built using TypeScript in Visual Studio (that's the .sln and .csproj files) and minified with Web Essentials, but feel free to use your tools of choice !

synchronizer-js's People

Contributors

sebastien-f avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.