Comparing version 5.0.0 to 6.0.0
104
core.js
'use strict'; | ||
var asap = require('asap') | ||
module.exports = require('./lib/core.js'); | ||
module.exports = Promise | ||
function Promise(fn) { | ||
if (typeof this !== 'object') throw new TypeError('Promises must be constructed via new') | ||
if (typeof fn !== 'function') throw new TypeError('not a function') | ||
var state = null | ||
var value = null | ||
var deferreds = [] | ||
var self = this | ||
this.then = function(onFulfilled, onRejected) { | ||
return new Promise(function(resolve, reject) { | ||
handle(new Handler(onFulfilled, onRejected, resolve, reject)) | ||
}) | ||
} | ||
function handle(deferred) { | ||
if (state === null) { | ||
deferreds.push(deferred) | ||
return | ||
} | ||
asap(function() { | ||
var cb = state ? deferred.onFulfilled : deferred.onRejected | ||
if (cb === null) { | ||
(state ? deferred.resolve : deferred.reject)(value) | ||
return | ||
} | ||
var ret | ||
try { | ||
ret = cb(value) | ||
} | ||
catch (e) { | ||
deferred.reject(e) | ||
return | ||
} | ||
deferred.resolve(ret) | ||
}) | ||
} | ||
function resolve(newValue) { | ||
try { //Promise Resolution Procedure: https://github.com/promises-aplus/promises-spec#the-promise-resolution-procedure | ||
if (newValue === self) throw new TypeError('A promise cannot be resolved with itself.') | ||
if (newValue && (typeof newValue === 'object' || typeof newValue === 'function')) { | ||
var then = newValue.then | ||
if (typeof then === 'function') { | ||
doResolve(then.bind(newValue), resolve, reject) | ||
return | ||
} | ||
} | ||
state = true | ||
value = newValue | ||
finale() | ||
} catch (e) { reject(e) } | ||
} | ||
function reject(newValue) { | ||
state = false | ||
value = newValue | ||
finale() | ||
} | ||
function finale() { | ||
for (var i = 0, len = deferreds.length; i < len; i++) | ||
handle(deferreds[i]) | ||
deferreds = null | ||
} | ||
doResolve(fn, resolve, reject) | ||
} | ||
function Handler(onFulfilled, onRejected, resolve, reject){ | ||
this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null | ||
this.onRejected = typeof onRejected === 'function' ? onRejected : null | ||
this.resolve = resolve | ||
this.reject = reject | ||
} | ||
/** | ||
* Take a potentially misbehaving resolver function and make sure | ||
* onFulfilled and onRejected are only called once. | ||
* | ||
* Makes no guarantees about asynchrony. | ||
*/ | ||
function doResolve(fn, onFulfilled, onRejected) { | ||
var done = false; | ||
try { | ||
fn(function (value) { | ||
if (done) return | ||
done = true | ||
onFulfilled(value) | ||
}, function (reason) { | ||
if (done) return | ||
done = true | ||
onRejected(reason) | ||
}) | ||
} catch (ex) { | ||
if (done) return | ||
done = true | ||
onRejected(ex) | ||
} | ||
} | ||
console.error('require("promise/core") is deprecated, use require("promise/lib/core") instead.'); |
182
index.js
'use strict'; | ||
//This file contains then/promise specific extensions to the core promise API | ||
var Promise = require('./core.js') | ||
var asap = require('asap') | ||
module.exports = Promise | ||
/* Static Functions */ | ||
function ValuePromise(value) { | ||
this.then = function (onFulfilled) { | ||
if (typeof onFulfilled !== 'function') return this | ||
return new Promise(function (resolve, reject) { | ||
asap(function () { | ||
try { | ||
resolve(onFulfilled(value)) | ||
} catch (ex) { | ||
reject(ex); | ||
} | ||
}) | ||
}) | ||
} | ||
} | ||
ValuePromise.prototype = Object.create(Promise.prototype) | ||
var TRUE = new ValuePromise(true) | ||
var FALSE = new ValuePromise(false) | ||
var NULL = new ValuePromise(null) | ||
var UNDEFINED = new ValuePromise(undefined) | ||
var ZERO = new ValuePromise(0) | ||
var EMPTYSTRING = new ValuePromise('') | ||
Promise.resolve = function (value) { | ||
if (value instanceof Promise) return value | ||
if (value === null) return NULL | ||
if (value === undefined) return UNDEFINED | ||
if (value === true) return TRUE | ||
if (value === false) return FALSE | ||
if (value === 0) return ZERO | ||
if (value === '') return EMPTYSTRING | ||
if (typeof value === 'object' || typeof value === 'function') { | ||
try { | ||
var then = value.then | ||
if (typeof then === 'function') { | ||
return new Promise(then.bind(value)) | ||
} | ||
} catch (ex) { | ||
return new Promise(function (resolve, reject) { | ||
reject(ex) | ||
}) | ||
} | ||
} | ||
return new ValuePromise(value) | ||
} | ||
Promise.from = Promise.cast = function (value) { | ||
var err = new Error('Promise.from and Promise.cast are deprecated, use Promise.resolve instead') | ||
err.name = 'Warning' | ||
console.warn(err.stack) | ||
return Promise.resolve(value) | ||
} | ||
Promise.denodeify = function (fn, argumentCount) { | ||
argumentCount = argumentCount || Infinity | ||
return function () { | ||
var self = this | ||
var args = Array.prototype.slice.call(arguments) | ||
return new Promise(function (resolve, reject) { | ||
while (args.length && args.length > argumentCount) { | ||
args.pop() | ||
} | ||
args.push(function (err, res) { | ||
if (err) reject(err) | ||
else resolve(res) | ||
}) | ||
fn.apply(self, args) | ||
}) | ||
} | ||
} | ||
Promise.nodeify = function (fn) { | ||
return function () { | ||
var args = Array.prototype.slice.call(arguments) | ||
var callback = typeof args[args.length - 1] === 'function' ? args.pop() : null | ||
try { | ||
return fn.apply(this, arguments).nodeify(callback) | ||
} catch (ex) { | ||
if (callback === null || typeof callback == 'undefined') { | ||
return new Promise(function (resolve, reject) { reject(ex) }) | ||
} else { | ||
asap(function () { | ||
callback(ex) | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
Promise.all = function () { | ||
var calledWithArray = arguments.length === 1 && Array.isArray(arguments[0]) | ||
var args = Array.prototype.slice.call(calledWithArray ? arguments[0] : arguments) | ||
if (!calledWithArray) { | ||
var err = new Error('Promise.all should be called with a single array, calling it with multiple arguments is deprecated') | ||
err.name = 'Warning' | ||
console.warn(err.stack) | ||
} | ||
return new Promise(function (resolve, reject) { | ||
if (args.length === 0) return resolve([]) | ||
var remaining = args.length | ||
function res(i, val) { | ||
try { | ||
if (val && (typeof val === 'object' || typeof val === 'function')) { | ||
var then = val.then | ||
if (typeof then === 'function') { | ||
then.call(val, function (val) { res(i, val) }, reject) | ||
return | ||
} | ||
} | ||
args[i] = val | ||
if (--remaining === 0) { | ||
resolve(args); | ||
} | ||
} catch (ex) { | ||
reject(ex) | ||
} | ||
} | ||
for (var i = 0; i < args.length; i++) { | ||
res(i, args[i]) | ||
} | ||
}) | ||
} | ||
Promise.reject = function (value) { | ||
return new Promise(function (resolve, reject) { | ||
reject(value); | ||
}); | ||
} | ||
Promise.race = function (values) { | ||
return new Promise(function (resolve, reject) { | ||
values.forEach(function(value){ | ||
Promise.resolve(value).then(resolve, reject); | ||
}) | ||
}); | ||
} | ||
/* Prototype Methods */ | ||
Promise.prototype.done = function (onFulfilled, onRejected) { | ||
var self = arguments.length ? this.then.apply(this, arguments) : this | ||
self.then(null, function (err) { | ||
asap(function () { | ||
throw err | ||
}) | ||
}) | ||
} | ||
Promise.prototype.nodeify = function (callback) { | ||
if (typeof callback != 'function') return this | ||
this.then(function (value) { | ||
asap(function () { | ||
callback(null, value) | ||
}) | ||
}, function (err) { | ||
asap(function () { | ||
callback(err) | ||
}) | ||
}) | ||
} | ||
Promise.prototype['catch'] = function (onRejected) { | ||
return this.then(null, onRejected); | ||
} | ||
module.exports = require('./lib/core.js') | ||
require('./lib/done.js') | ||
require('./lib/es6-extensions.js') | ||
require('./lib/node-extensions.js') |
{ | ||
"name": "promise", | ||
"version": "5.0.0", | ||
"version": "6.0.0", | ||
"description": "Bare bones Promises/A+ implementation", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha -R spec --timeout 200 --slow 99999", | ||
"test": "mocha --timeout 200 --slow 99999", | ||
"test-resolve": "mocha test/resolver-tests.js -R spec --timeout 200 --slow 999999", | ||
@@ -9,0 +9,0 @@ "test-extensions": "mocha test/extensions-tests.js -R spec --timeout 200 --slow 999999" |
@@ -8,5 +8,5 @@ <a href="http://promises-aplus.github.com/promises-spec"><img src="http://promises-aplus.github.com/promises-spec/assets/logo-small.png" align="right" /></a> | ||
[![Build Status](https://travis-ci.org/then/promise.png)](https://travis-ci.org/then/promise) | ||
[![Dependency Status](https://gemnasium.com/then/promise.png)](https://gemnasium.com/then/promise) | ||
[![NPM version](https://badge.fury.io/js/promise.png)](http://badge.fury.io/js/promise) | ||
[![Build Status](https://img.shields.io/travis/then/promise/master.svg)](https://travis-ci.org/then/promise) | ||
[![Dependency Status](https://img.shields.io/gemnasium/then/promise.svg)](https://gemnasium.com/then/promise) | ||
[![NPM version](https://img.shields.io/npm/v/promise.svg)](https://www.npmjs.org/package/promise) | ||
@@ -21,3 +21,3 @@ ## Installation | ||
You can use browserify on the client, or use the pre-compiled script that acts as a pollyfill. | ||
You can use browserify on the client, or use the pre-compiled script that acts as a polyfill. | ||
@@ -28,2 +28,8 @@ ```html | ||
Note that the [es5-shim](https://github.com/es-shims/es5-shim) must be loaded before this library to support browsers pre IE9. | ||
```html | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/3.4.0/es5-shim.min.js"></script> | ||
``` | ||
## Usage | ||
@@ -30,0 +36,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18848
13
274
218
1