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

basic-deferred

v0.0.5

Published

Deferred in the most basic way

Downloads

11

Readme

Basic deferred

Is the way to manage asynchronous calls. Basic deferred is a deferred library developed with simplicity in mind. Is a factory function that returns a chainable utility object with methods to register multiple callbacks into callback queues, invoke callback queues, and relay the success or failure state of any synchronous or asynchronous function.

Installation

$ npm install basic-deferred

Typical deferred function

var Deferred = require('basic-deferred');
function dummyFn(success) {
  var deferred = new Deferred();
  if(success) {
    deferred.resolve("Its allright!");
  } else {
    deferred.reject("Not okey");
  }
  return deferred.promise();
}

Create the deferred object

The new Deferred() creates a new deferred object. The deferred object can be created with options in which we can set if we want information about which parameter was rejected and if we continue processing the parameters after one of them was rejected. By default verbose and processOnFail are false.

new Deferred({verbose: true, processOnFail: true})
  • verbose: Indicates if you want information about which parameter was rejected in a deferred.when() function.
  • processOnFail: If is true, it will process all parameters no matter if one was rejected.

A Deferred object starts in the pending state. Any callbacks added to the object with deferred.then(), deferred.done(), or deferred.fail() are queued to be executed later. Calling deferred.resolve transitions the Deferred into the resolved state and immediately executes any done callback that are set. In the same way, calling deferred.reject put Deferred into a failed state and immediately executes any fail callback.

var Deferred = require('basic-deferred');

var dummyFn = function() {
  var deferred = new Deferred();
  setTimeout(function() {
    deferred.resolve("Hello world!");
  }, 2000);
  return deferred.promise();
};

dummyFn().done(function(res) {
  console.log(res);
});

// Hello world!

There is one exception, deferred.when; here Deferred is going to wait until every single parameter changes to a resolved state and then will execute the "then" callback with all the responses.

var Deferred = require('basic-deferred');

var dummyFn = function(value, isSuccess, time) {
  var deferred = new Deferred();
  setTimeout(function() {
    isSuccess ? deferred.resolve(value) : deferred.reject("failed");
  }, time);
  return deferred.promise();
};

var a = dummyFn("Hello ", true, 3000); // 3 seconds
var b = dummyFn("Marty Mc Fly!", true, 1000); // 1 second
var c = dummyFn(", Welcome to 2015, sorry we don't have hoverboards yet!", true, 5000); // 5 seconds

var deferred = new Deferred();
deferred.when(a, b, c)
  .then(function(aRes, bRes, cRes) {
    console.log(aRes, bRes, cRes);
  })
  .fail(function() {
    console.log(arguments);
  });

// Hello Marty Mc Fly! Welcome to 2015, sorry we don't have hoverboards yet! (after 5 seconds).

In the previous example, we manage all to result successfully, but what happens if we alter that and make b fail? The response will be fired by the fail callback:

// { '0': undefined, '1': 'failed' }

What is that undefined?, the second param fails, and too soon. So the first never was processed and, because of that, is undefined. If we want to process all no matter what we can set to true the processOnFail option.

Version

0.0.5