Git Product home page Git Product logo

jpromise's Introduction

jPromise

A Promise Library that is actually spec compliant with the latest versions of the spec...

A library conforming to the Promise/A spec.

Tested in Chrome, Firefox, Safari, and IE8/9/10.

This is a promise library that is speedy quick, doesn't hold onto scopes everywhere all willy-nilly, and is also quite flexible.

  • p Constructor
    • Creates a new deferred object
  • dfd.promise()
    • Returns the promise for a given deferred object. The promise object is not able to access some of the deferrer's methods, such as resolve and reject.
  • dfd.resolve(arg)
    • Resolves the deferred object. When the deferred object gets resolved, callbacks on dfd.then, dfd.done, and dfd.always will get called with arg.
  • dfd.reject(arg)
    • Rejects the deferred object. When the deferred object gets resolved, callbacks on dfd.then, dfd.fail, and dfd.always will get called with arg.
  • dfd.progress(arg)
    • Progresses the deferred object (like a resolve but can be called multiple times). When the deferred object gets resolved, callbacks on dfd.notify will get called with arg.
  • dfd.done(cb) and promise.done(cb)
    • Creates a callback for when the deferred object gets resolved. The cb function will get called with the arg that the deferred object was resolved with.
  • dfd.fail(cb) and promise.fail(cb)
    • Creates a callback for when the deferred object gets rejected. The cb function will get called with the arg that the deferred object was rejected with.
  • dfd.progress(cb) and promise.progress(cb)
    • Creates a callback for when the deferred object gets notified. The cb function will get called with the arg that the deferred object was notified with.

Example:

//Stashes on the global _ on the property Dfd...
//The closure also returns the Dfd constructor and could be munged to work in AMD/Common if we gave a...
var p = require('jpromise');

var dfd = new p();

var pro = dfd.promise();

pro.always(function(data) {
  console.log("always", data);
});

pro.done(function(data) {
  console.log("done", data);
});

pro.fail(function(data) {
  console.log("fail", data);
});

pro.progress(function(data) {
  console.log("progress", data);
});

setTimeout(function() {
  dfd.notify("foo");

  console.log("state", dfd.state());

  dfd.resolve("bar");

  pro.done(function(data) {
    console.log("after Done", data);
  });

  console.log("state", dfd.state());

}, 500);

//returns in console:
// progress "foo"
// state 0
// always "bar"
// done "bar"
// after Done "bar"
// state 1

Notes

  • You can done/fail/progress/then on the dfd as well as the promise, but you can only resolve/reject/notify on the dfd
  • You cannot escalate the promise into the dfd, it is a one way street
  • You can get the promise from the dfd multiple times and it will always return the same promise instance
  • You can bind at any point even after a dfd has resolved/rejected and it will instantly return
  • This sucker is about 3x as fast/efficient as jQuery's Deferred piece clocking in at about 100k ops a second

jpromise's People

Contributors

jessieamorris avatar nickhsharp avatar

Watchers

 avatar  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.