@blinkmobile/jqpromise

wrap jQuery.Deferred to make it more like native Promises


Keywords
coerce, cast, types
License
BSD-3-Clause
Install
npm install @blinkmobile/jqpromise@1.0.1

Documentation

jqpromise.js

wrap jQuery.Deferred to make it more like native Promises

npm module travis-ci

What is this?

This is handy for older projects that still have jQuery. Why load a separate Promises poly-fill when there's a "good enough" implementation buried in jQuery?

This wrapper exports a new constructor that internally uses $.Deferred. The difference is the argument is the same sort of callback that native Promises uses, instead of the $.Deferred argument.

Usage

// $.Deferred
var dfrd = new $.Deferred(function (d) {
  setTimeout(function () {
    d.resolve(); // or d.reject();
  }, 0);
});

// native Promise
var promise = new Promise(function (resolve, reject) {
  setTimeout(function () {
    resolve(); // or reject();
  }, 0);
});

// JQPromise
var jqp = new JQPromise(function (resolve, reject) {
  setTimeout(function () {
    resolve(); // or reject();
  }, 0);
});

// native Promise falling back to JQPromise
var P = window.Promise || window.JQPromise;
var p = new P(function (resolve, reject) {
  setTimeout(function () {
    resolve(); // or reject();
  }, 0);
});