Comparing version 3.2.0 to 4.0.0
58
core.js
@@ -1,11 +0,10 @@ | ||
'use strict' | ||
'use strict'; | ||
var nextTick = require('./lib/next-tick') | ||
var asap = require('asap') | ||
module.exports = Promise | ||
function Promise(fn) { | ||
if (!(this instanceof Promise)) return new 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 delegating = false | ||
var value = null | ||
@@ -26,3 +25,3 @@ var deferreds = [] | ||
} | ||
nextTick(function() { | ||
asap(function() { | ||
var cb = state ? deferred.onFulfilled : deferred.onRejected | ||
@@ -46,10 +45,2 @@ if (cb === null) { | ||
function resolve(newValue) { | ||
if (delegating) | ||
return | ||
resolve_(newValue) | ||
} | ||
function resolve_(newValue) { | ||
if (state !== null) | ||
return | ||
try { //Promise Resolution Procedure: https://github.com/promises-aplus/promises-spec#the-promise-resolution-procedure | ||
@@ -60,4 +51,3 @@ if (newValue === self) throw new TypeError('A promise cannot be resolved with itself.') | ||
if (typeof then === 'function') { | ||
delegating = true | ||
then.call(newValue, resolve_, reject_) | ||
doResolve(then.bind(newValue), resolve, reject) | ||
return | ||
@@ -69,14 +59,6 @@ } | ||
finale() | ||
} catch (e) { reject_(e) } | ||
} catch (e) { reject(e) } | ||
} | ||
function reject(newValue) { | ||
if (delegating) | ||
return | ||
reject_(newValue) | ||
} | ||
function reject_(newValue) { | ||
if (state !== null) | ||
return | ||
state = false | ||
@@ -93,4 +75,3 @@ value = newValue | ||
try { fn(resolve, reject) } | ||
catch(e) { reject(e) } | ||
doResolve(fn, resolve, reject) | ||
} | ||
@@ -105,1 +86,26 @@ | ||
} | ||
/** | ||
* 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) | ||
} | ||
} |
98
index.js
@@ -1,2 +0,2 @@ | ||
'use strict' | ||
'use strict'; | ||
@@ -6,3 +6,3 @@ //This file contains then/promise specific extensions to the core promise API | ||
var Promise = require('./core.js') | ||
var nextTick = require('./lib/next-tick') | ||
var asap = require('asap') | ||
@@ -13,7 +13,52 @@ module.exports = Promise | ||
Promise.from = function (value) { | ||
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.from = Promise.cast = function (value) { | ||
if (value instanceof Promise) return value | ||
return new Promise(function (resolve) { resolve(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.denodeify = function (fn) { | ||
Promise.denodeify = function (fn, argumentCount) { | ||
argumentCount = argumentCount || Infinity | ||
return function () { | ||
@@ -23,2 +68,5 @@ var self = this | ||
return new Promise(function (resolve, reject) { | ||
while (args.length && args.length > argumentCount) { | ||
args.pop() | ||
} | ||
args.push(function (err, res) { | ||
@@ -39,6 +87,6 @@ if (err) reject(err) | ||
} catch (ex) { | ||
if (callback == null) { | ||
if (callback === null || typeof callback == 'undefined') { | ||
return new Promise(function (resolve, reject) { reject(ex) }) | ||
} else { | ||
nextTick(function () { | ||
asap(function () { | ||
callback(ex) | ||
@@ -85,3 +133,3 @@ }) | ||
self.then(null, function (err) { | ||
nextTick(function () { | ||
asap(function () { | ||
throw err | ||
@@ -91,14 +139,40 @@ }) | ||
} | ||
Promise.prototype.nodeify = function (callback) { | ||
if (callback == null) return this | ||
if (callback === null || typeof callback == 'undefined') return this | ||
this.then(function (value) { | ||
nextTick(function () { | ||
asap(function () { | ||
callback(null, value) | ||
}) | ||
}, function (err) { | ||
nextTick(function () { | ||
asap(function () { | ||
callback(err) | ||
}) | ||
}) | ||
} | ||
} | ||
Promise.prototype.catch = function (onRejected) { | ||
return this.then(null, onRejected); | ||
} | ||
Promise.resolve = function (value) { | ||
return new Promise(function (resolve) { | ||
resolve(value); | ||
}); | ||
} | ||
Promise.reject = function (value) { | ||
return new Promise(function (resolve, reject) { | ||
reject(value); | ||
}); | ||
} | ||
Promise.race = function (values) { | ||
return new Promise(function (resolve, reject) { | ||
values.map(function(value){ | ||
Promise.cast(value).then(resolve, reject); | ||
}) | ||
}); | ||
} |
{ | ||
"name": "promise", | ||
"version": "3.2.0", | ||
"version": "4.0.0", | ||
"description": "Bare bones Promises/A+ implementation", | ||
@@ -21,3 +21,6 @@ "main": "index.js", | ||
"mocha": "*" | ||
}, | ||
"dependencies": { | ||
"asap": "~1.0.0" | ||
} | ||
} |
@@ -14,5 +14,13 @@ <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> | ||
Server: | ||
**Server:** | ||
$ npm install promise | ||
**Client:** | ||
You can use browserify on the client, or download a standalone version from [www.promisejs.org](http://www.promisejs.org/implementations/#i-promise) | ||
```html | ||
<script src="http://www.promisejs.org/implementations/promise/promise-3.2.0.js"></script> | ||
``` | ||
@@ -42,5 +50,5 @@ ## Usage | ||
### Promise(resolver) | ||
### new Promise(resolver) | ||
This creates and returns a new promise. The `new` keyword before `Promise` is optional. `resolver` must be a function. The `resolver` function is passed two arguments: | ||
This creates and returns a new promise. `resolver` must be a function. The `resolver` function is passed two arguments: | ||
@@ -134,3 +142,3 @@ 1. `resolve` should be called with a single argument. If it is called with a non-promise value then the promise is fulfilled with that value. If it is called with a promise (A) then the returned promise takes on the state of that new promise (A). | ||
```javascript | ||
funciton awesomeAPI(foo, bar, callback) { | ||
function awesomeAPI(foo, bar, callback) { | ||
return internalAPI(foo, bar) | ||
@@ -137,0 +145,0 @@ .then(parseResult) |
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
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
16896
245
202
1
+ Addedasap@~1.0.0
+ Addedasap@1.0.0(transitive)