Skip to content

A JS helper for waiting for multiple async operations to finish.

License

Notifications You must be signed in to change notification settings

BeneathTheInk/asyncwait

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Async Wait

This is a small JavaScript utility that waits for many async operations to finish. This is very similar to something like async.map or Promise.all, but with a looser API.

Install

Download the latest version from our release page and use via a script tag.

<script type="text/javascript" src="asyncwait.js"></script>
asyncWait(function(){});

If using Browserify or Node.js, install with NPM.

$ npm install asyncwait
var asyncWait = require("asyncwait");

Usage

Call asyncWait() with an onEmpty callback. The callback will be called when all async operations being waited on have settled and completed. The returned method can be used to generate wait functions, which prevent the onEmpty callback from being called.

Here is a really basic example.

var generateWait = asyncWait(function() {
    console.log("done");
});

var waitFn = generateWait();
setTimeout(waitFn, 100);
setTimeout(waitFn, 500); // calling twice does nothing

It's mostly useful for waiting for many async operations to finish. The onEmpty callback is run immediately after the last wait function is executed.

var generateWait = asyncWait(function() {
    console.log("done");
});

myFirstAsyncThing(generateWait());
mySecondAsyncThing(generateWait());
myThirdAsyncThing(generateWait());

You can also pass a callback through the wait function. The callback is run prior to the onEmpty callback. This means you can generate and nest more wait functions within it.

var generateWait = asyncWait(function() {
    console.log("done");
});

// callback is run as if generateWait wasn't even there
somethingAsync(generateWait(function(err) {
    if (err) dealWithError(err);
    
    // onEmpty waits for this one too
    myOtherAsyncThing(generateWait());
}));

This style of async control flow is really flexible and plays nice with everything. Here's an example with ES6 Promises:

var generateWait = asyncWait(function() {
    console.log("done");
});

// we must add both a resolve and reject callback
// so our onEmpty function is guaranteed to run
// regardless of the outcome.
var done = generateWait();
myAsyncTask().then(function() {
    console.log("success");
    done();
}, function(err) {
    console.error(err);
    done();
});

About

A JS helper for waiting for multiple async operations to finish.

Resources

License

Stars

Watchers

Forks

Packages

No packages published