Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

promiser

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promiser

Manager of jQuery deferreds by name

  • 0.2.2
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
11
Maintainers
1
Weekly downloads
 
Created
Source

Promiser

promiser is a simple manager of deferreds by name. Rather than creating and maintaining references to jQuery.Deferred objects, simply register a handler by name and resolve or reject it by name.

promiser.done('test', function() {
    console.log('hello world');
});

promiser.resolve('test');

promiser.isResolved('test'); // true

The promiser API includes all the methods on the jQuery Deferred object.

Chaining works as expected:

promiser
    .done('test1', function() { ... })
    .done('test2', function() { ... })
    .done('test3', function() { ... });

For a more elegant approach, an object can be passed:

promiser.done({
    test1: function() { ... },
    test2: function() { ... },
    test3: function() { ... }
});

It supports the when method:

promiser.when('test1', 'test2', 'test3', function() {
    console.log('all done!');
});

Promiser can manage other deferred for you:

promiser.manage('ajax', $.ajax({ ... }));

It can also stop managing a deferred:

var xhr = promiser.unmanage('ajax');

You can even reset a deferred. This provides a clean alternative to passing around references to deferred objects:

promiser.reset('test1');

Deferreds can be watched for when they are initially created. This makes it easy to only execute something if another object needs it:

// Watch for the first time the 'lazy-data' deferrred it bound to
promiser.watch('lazy-data', function() {
    $.ajax({
        success: function(data) {
            promiser.resolve('lazy-data', data);
        },
        error: function(xhr, text, err) {
            promiser.reject('lazy-data', xhr, text, err);
        }
    });
});

// This executes the watch handler above
promiser.done('lazy-data', function(data) {
    // do something...
});

Install

Bower

bower install promiser

NPM

npm install promiser

Setup

promiser.js works in the browser as well as the Node and AMD environments.

Usage

The promiser object can be used three ways:

Singleton

// It can be used directly
promiser.done('foo', function() { ... });

Constructor

// Create promiser objects
var p1 = new promiser;
p1 instanceof promiser; // true

Function

// Create a new plain object
var p1 = promiser();

// Extend an existing object
var p2 = promiser({});

Keywords

FAQs

Package last updated on 19 Aug 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc