npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

underscore-fire

v0.0.3

Published

Fires multiple asynchronous functions with a single callback

Downloads

7

Readme


| | | | | | | ()
| | | |
__ | | ___ _ __ ___ ___ ___ _ __ ___ | | _ _ __ ___ | | | | ' \ / ` |/ _ \ '__/ |/ / _ | '/ _ \ | _| | | '/ _
| |
| | | | | (
| | / | _ \ (| () | | | __/ | | | | | | / _/|| ||_,|___|| |/____/|| _| _| ||| _|

This underscore add-on was written to allow you to fire multiple functions at the same time and get a single callback when they are all completed. There is no reliance on the DOM, so you can use it in the browser or in Node.

Please note that there is currently no support for chaining function calls and that all of the functions will be fired at the same time.

The only hard dependency is underscore or lodash.

API

While the _.fire method is very powerful, it only has three parameters:

_.fire( array [functions to fire], function [callback], function or array [filter] );

The functions to fire will each be called by passing in any data from the optional filter parameter. Once all of the functions have been completed, the callback function will fire with an array of all of the results from the functions as its only parameter.

Maybe a few examples would be helpful...

Examples

Firing a single function

You'd probably never need to fire a single function with Underscore-Fire, but this is how you would do it.

var fns = function () {
    return "hello world";
};

_.fire( fns, function (data) {
    // ["hello world"];
    console.log(data);
});

Firing multiple functions

We can fire multiple functions. Once they are all fired, the callback will contain an array of all three results.

var fns = [function () {
    return "hello world";
}, function () {
    return "hello moon";
}, function () {
    return "hello sun";
}];

_.fire( fns, function (data) {
    // ["hello world", "hello moon", "hello sun"];
    console.log(data);
});

Asynchronous callbacks

Every function has a "done" function as its first parameter. Instead of using return, you can fire the done function when you are ready. This is useful for timeouts, ajax calls and asynchronous node processes. Note that even though the third function will complete before the first function, the results are still returned in the order the functions are added to the array.

var fns = [function (done) {
    setTimeout(function () {
       done("hello world");
    }, 2000);
}, function (done) {
    setTimeout(function () {
       done("hello moon");
    }, 1500);
}, function (done) {
    setTimeout(function () {
       done("hello sun");
    }, 1000);
}];

_.fire( fns, function (data) {
    // ["hello world", "hello moon", "hello sun"];
    console.log(data);
});

Passing in parameters

Every function has a "done" function as its first parameter. Instead of using return, you can fire the done function when you are ready. This is useful for timeouts, ajax calls and asynchronous node processes. Note that even though the third function will complete before the first function, the results are still returned in the order the functions are added to the array. You can pass up to 15 parameters into the filter array.

var fns = [function (done, hello, exclamation) {
    return hello + " world" + exclamation;

}, function (done, hello, exclamation) {
    return hello + " moon" + exclamation;

}, function (done, hello, exclamation) {
    return hello + " sun" + exclamation;
}];

_.fire( fns, function (data) {

    // ["hello world!", "hello moon!", "hello sun!"];
    console.log(data);

}, ["hello", "!"]);

Passing in different parameters for each function

If you provide an array of filters, each one will be applied to the function with the same index.

var fns = [function (done, hello) {
    return hello;

}, function (done, hello) {
    return hello;
    
}, function (done, hello) {
    return hello;
}];

var filters = [
  ["hello world"],
  ["hello moon"],
  ["hello sun"]
];

_.fire( fns, function (data) {

    // ["hello world", "hello moon", "hello sun"];
    console.log(data);

}, filters);

Passing the filters as a function

You can use a function for the filters instead of an array. The function will include a "done" function to use as a callback and the index of the function being called.

var fns = [function (done, hello) {
    return hello;

}, function (done, hello) {
    return hello;
    
}, function (done, hello) {
    return hello;
}];

var filters = function (done, index) {
  switch(index) {
    
    case 0:
      done("hello world");
      break;

    case 1:
      done("hello moon");
      break;
    
    case 2:
      done("hello sun");
      break;
  }
};

_.fire( fns, function (data) {

    // ["hello world", "hello moon", "hello sun"];
    console.log(data);

}, filters);