Comparing version 2.0.0 to 2.0.1
@@ -7,5 +7,6 @@ "use strict"; | ||
function all(promises) { | ||
if(toString.call(promises) !== "[object Array]") { | ||
if (Object.prototype.toString.call(promises) !== "[object Array]") { | ||
throw new TypeError('You must pass an array to all.'); | ||
} | ||
return new Promise(function(resolve, reject) { | ||
@@ -12,0 +13,0 @@ var results = [], remaining = promises.length, |
"use strict"; | ||
var browserGlobal = (typeof window !== 'undefined') ? window : {}; | ||
var BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver; | ||
var async; | ||
if (typeof process !== 'undefined' && | ||
{}.toString.call(process) === '[object process]') { | ||
async = function(callback, binding) { | ||
// old node | ||
function useNextTick() { | ||
return function(callback, arg) { | ||
process.nextTick(function() { | ||
callback.call(binding); | ||
callback(arg); | ||
}); | ||
}; | ||
} else if (BrowserMutationObserver) { | ||
} | ||
// node >= 0.10.x | ||
function useSetImmediate() { | ||
return function(callback, arg) { | ||
/* global setImmediate */ | ||
setImmediate(function(){ | ||
callback(arg); | ||
}); | ||
}; | ||
} | ||
function useMutationObserver() { | ||
var queue = []; | ||
@@ -22,4 +33,4 @@ | ||
toProcess.forEach(function(tuple) { | ||
var callback = tuple[0], binding = tuple[1]; | ||
callback.call(binding); | ||
var callback = tuple[0], arg= tuple[1]; | ||
callback(arg); | ||
}); | ||
@@ -35,12 +46,14 @@ }); | ||
observer = null; | ||
}); | ||
}, false); | ||
async = function(callback, binding) { | ||
queue.push([callback, binding]); | ||
return function(callback, arg) { | ||
queue.push([callback, arg]); | ||
element.setAttribute('drainQueue', 'drainQueue'); | ||
}; | ||
} else { | ||
async = function(callback, binding) { | ||
} | ||
function useSetTimeout() { | ||
return function(callback, arg) { | ||
setTimeout(function() { | ||
callback.call(binding); | ||
callback(arg); | ||
}, 1); | ||
@@ -50,3 +63,13 @@ }; | ||
if (typeof setImmediate === 'function') { | ||
async = useSetImmediate(); | ||
} else if (typeof process !== 'undefined' && {}.toString.call(process) === '[object process]') { | ||
async = useNextTick(); | ||
} else if (BrowserMutationObserver) { | ||
async = useMutationObserver(); | ||
} else { | ||
async = useSetTimeout(); | ||
} | ||
exports.async = async; |
@@ -5,5 +5,10 @@ "use strict"; | ||
function defer() { | ||
var deferred = {}; | ||
var deferred = { | ||
// pre-allocate shape | ||
resolve: undefined, | ||
reject: undefined, | ||
promise: undefined | ||
}; | ||
var promise = new Promise(function(resolve, reject) { | ||
deferred.promise = new Promise(function(resolve, reject) { | ||
deferred.resolve = resolve; | ||
@@ -13,3 +18,2 @@ deferred.reject = reject; | ||
deferred.promise = promise; | ||
return deferred; | ||
@@ -16,0 +20,0 @@ } |
@@ -93,2 +93,7 @@ "use strict"; | ||
isRejected: undefined, | ||
isFulfilled: undefined, | ||
rejectedReason: undefined, | ||
fulfillmentValue: undefined, | ||
then: function(done, fail) { | ||
@@ -100,4 +105,4 @@ this.off('error', onerror); | ||
if (this.isFulfilled) { | ||
config.async(function() { | ||
invokeCallback('resolve', thenPromise, done, { detail: this.fulfillmentValue }); | ||
config.async(function(promise) { | ||
invokeCallback('resolve', thenPromise, done, { detail: promise.fulfillmentValue }); | ||
}, this); | ||
@@ -107,4 +112,4 @@ } | ||
if (this.isRejected) { | ||
config.async(function() { | ||
invokeCallback('reject', thenPromise, fail, { detail: this.rejectedReason }); | ||
config.async(function(promise) { | ||
invokeCallback('reject', thenPromise, fail, { detail: promise.rejectedReason }); | ||
}, this); | ||
@@ -136,15 +141,18 @@ } | ||
function handleThenable(promise, value) { | ||
var then = null; | ||
var then = null, | ||
resolved; | ||
if (objectOrFunction(value)) { | ||
try { | ||
then = value.then; | ||
} catch(e) { | ||
reject(promise, e); | ||
return true; | ||
try { | ||
if (promise === value) { | ||
throw new TypeError("A promises callback cannot return that same promise."); | ||
} | ||
if (isFunction(then)) { | ||
try { | ||
if (objectOrFunction(value)) { | ||
then = value.then; | ||
if (isFunction(then)) { | ||
then.call(value, function(val) { | ||
if (resolved) { return true; } | ||
resolved = true; | ||
if (value !== val) { | ||
@@ -156,9 +164,14 @@ resolve(promise, val); | ||
}, function(val) { | ||
if (resolved) { return true; } | ||
resolved = true; | ||
reject(promise, val); | ||
}); | ||
} catch (e) { | ||
reject(promise, e); | ||
return true; | ||
} | ||
return true; | ||
} | ||
} catch (error) { | ||
reject(promise, error); | ||
return true; | ||
} | ||
@@ -165,0 +178,0 @@ |
"use strict"; | ||
var Promise = require("./promise").Promise; | ||
function objectOrFunction(x) { | ||
return typeof x === "function" || (typeof x === "object" && x !== null); | ||
} | ||
function reject(reason) { | ||
@@ -11,0 +5,0 @@ return new Promise(function (resolve, reject) { |
"use strict"; | ||
var Promise = require("./promise").Promise; | ||
function objectOrFunction(x) { | ||
return typeof x === "function" || (typeof x === "object" && x !== null); | ||
} | ||
function resolve(thenable) { | ||
if (thenable instanceof Promise) { | ||
return thenable; | ||
} | ||
var promise = new Promise(function(resolve, reject) { | ||
var then; | ||
try { | ||
if ( objectOrFunction(thenable) ) { | ||
then = thenable.then; | ||
if (typeof then === "function") { | ||
then.call(thenable, resolve, reject); | ||
} else { | ||
resolve(thenable); | ||
} | ||
} else { | ||
resolve(thenable); | ||
} | ||
} catch(error) { | ||
reject(error); | ||
} | ||
return new Promise(function(resolve, reject) { | ||
resolve(thenable); | ||
}); | ||
return promise; | ||
} | ||
@@ -37,0 +9,0 @@ |
@@ -5,5 +5,7 @@ module.exports = function(grunt) { | ||
grunt.loadNpmTasks('grunt-microlib'); | ||
grunt.loadNpmTasks('grunt-s3'); | ||
// Custom phantomjs test task | ||
this.registerTask('test:phantom', "Runs tests through the command line using PhantomJS", ['build', 'tests', 'mocha_phantomjs']); | ||
this.registerTask('test:phantom', "Runs tests through the command line using PhantomJS", [ | ||
'build', 'tests', 'mocha_phantomjs']); | ||
@@ -26,30 +28,10 @@ // Custom Node test task | ||
}, | ||
env: process.env, | ||
pkg: grunt.file.readJSON('package.json'), | ||
browserify: { | ||
tests: { | ||
src: ['test/test-adapter.js', | ||
'node_modules/promises-aplus-tests/lib/tests/**/*.js', | ||
'node_modules/promises-aplus-tests/node_modules/sinon/lib/{sinon.js,sinon/*.js}'], | ||
dest: 'tmp/tests-bundle.js' | ||
} | ||
}, | ||
mocha_phantomjs: { | ||
phantom: { | ||
options: { | ||
urls: ["test/index.html"], | ||
} | ||
} | ||
}, | ||
mochaTest: { | ||
test: { | ||
src: ['test/vendor/assert.js', 'test/test-adapter.js', 'node_modules/promises-aplus-tests/lib/tests/**/*.js', 'tmp/tests.cjs.js'], | ||
options: { | ||
reporter: 'spec' | ||
}, | ||
} | ||
} | ||
mochaTest: require('./options/mocha_test.js'), | ||
browserify: require('./options/browserify.js'), | ||
mocha_phantomjs: require('./options/mocha_phantom.js'), | ||
s3: require('./options/s3'), | ||
}; | ||
@@ -56,0 +38,0 @@ |
@@ -6,5 +6,6 @@ /* global toString */ | ||
function all(promises) { | ||
if(toString.call(promises) !== "[object Array]") { | ||
if (Object.prototype.toString.call(promises) !== "[object Array]") { | ||
throw new TypeError('You must pass an array to all.'); | ||
} | ||
return new Promise(function(resolve, reject) { | ||
@@ -11,0 +12,0 @@ var results = [], remaining = promises.length, |
var browserGlobal = (typeof window !== 'undefined') ? window : {}; | ||
var BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver; | ||
var async; | ||
if (typeof process !== 'undefined' && | ||
{}.toString.call(process) === '[object process]') { | ||
async = function(callback, binding) { | ||
// old node | ||
function useNextTick() { | ||
return function(callback, arg) { | ||
process.nextTick(function() { | ||
callback.call(binding); | ||
callback(arg); | ||
}); | ||
}; | ||
} else if (BrowserMutationObserver) { | ||
} | ||
// node >= 0.10.x | ||
function useSetImmediate() { | ||
return function(callback, arg) { | ||
/* global setImmediate */ | ||
setImmediate(function(){ | ||
callback(arg); | ||
}); | ||
}; | ||
} | ||
function useMutationObserver() { | ||
var queue = []; | ||
@@ -21,4 +32,4 @@ | ||
toProcess.forEach(function(tuple) { | ||
var callback = tuple[0], binding = tuple[1]; | ||
callback.call(binding); | ||
var callback = tuple[0], arg= tuple[1]; | ||
callback(arg); | ||
}); | ||
@@ -34,12 +45,14 @@ }); | ||
observer = null; | ||
}); | ||
}, false); | ||
async = function(callback, binding) { | ||
queue.push([callback, binding]); | ||
return function(callback, arg) { | ||
queue.push([callback, arg]); | ||
element.setAttribute('drainQueue', 'drainQueue'); | ||
}; | ||
} else { | ||
async = function(callback, binding) { | ||
} | ||
function useSetTimeout() { | ||
return function(callback, arg) { | ||
setTimeout(function() { | ||
callback.call(binding); | ||
callback(arg); | ||
}, 1); | ||
@@ -49,2 +62,12 @@ }; | ||
if (typeof setImmediate === 'function') { | ||
async = useSetImmediate(); | ||
} else if (typeof process !== 'undefined' && {}.toString.call(process) === '[object process]') { | ||
async = useNextTick(); | ||
} else if (BrowserMutationObserver) { | ||
async = useMutationObserver(); | ||
} else { | ||
async = useSetTimeout(); | ||
} | ||
export { async }; |
import { Promise } from "./promise"; | ||
function defer() { | ||
var deferred = {}; | ||
var deferred = { | ||
// pre-allocate shape | ||
resolve: undefined, | ||
reject: undefined, | ||
promise: undefined | ||
}; | ||
var promise = new Promise(function(resolve, reject) { | ||
deferred.promise = new Promise(function(resolve, reject) { | ||
deferred.resolve = resolve; | ||
@@ -11,3 +16,2 @@ deferred.reject = reject; | ||
deferred.promise = promise; | ||
return deferred; | ||
@@ -14,0 +18,0 @@ } |
@@ -92,2 +92,7 @@ import { config } from "./config"; | ||
isRejected: undefined, | ||
isFulfilled: undefined, | ||
rejectedReason: undefined, | ||
fulfillmentValue: undefined, | ||
then: function(done, fail) { | ||
@@ -99,4 +104,4 @@ this.off('error', onerror); | ||
if (this.isFulfilled) { | ||
config.async(function() { | ||
invokeCallback('resolve', thenPromise, done, { detail: this.fulfillmentValue }); | ||
config.async(function(promise) { | ||
invokeCallback('resolve', thenPromise, done, { detail: promise.fulfillmentValue }); | ||
}, this); | ||
@@ -106,4 +111,4 @@ } | ||
if (this.isRejected) { | ||
config.async(function() { | ||
invokeCallback('reject', thenPromise, fail, { detail: this.rejectedReason }); | ||
config.async(function(promise) { | ||
invokeCallback('reject', thenPromise, fail, { detail: promise.rejectedReason }); | ||
}, this); | ||
@@ -135,15 +140,18 @@ } | ||
function handleThenable(promise, value) { | ||
var then = null; | ||
var then = null, | ||
resolved; | ||
if (objectOrFunction(value)) { | ||
try { | ||
then = value.then; | ||
} catch(e) { | ||
reject(promise, e); | ||
return true; | ||
try { | ||
if (promise === value) { | ||
throw new TypeError("A promises callback cannot return that same promise."); | ||
} | ||
if (isFunction(then)) { | ||
try { | ||
if (objectOrFunction(value)) { | ||
then = value.then; | ||
if (isFunction(then)) { | ||
then.call(value, function(val) { | ||
if (resolved) { return true; } | ||
resolved = true; | ||
if (value !== val) { | ||
@@ -155,9 +163,14 @@ resolve(promise, val); | ||
}, function(val) { | ||
if (resolved) { return true; } | ||
resolved = true; | ||
reject(promise, val); | ||
}); | ||
} catch (e) { | ||
reject(promise, e); | ||
return true; | ||
} | ||
return true; | ||
} | ||
} catch (error) { | ||
reject(promise, error); | ||
return true; | ||
} | ||
@@ -164,0 +177,0 @@ |
import { Promise } from "./promise"; | ||
function objectOrFunction(x) { | ||
return typeof x === "function" || (typeof x === "object" && x !== null); | ||
} | ||
function reject(reason) { | ||
@@ -10,0 +4,0 @@ return new Promise(function (resolve, reject) { |
import { Promise } from "./promise"; | ||
function objectOrFunction(x) { | ||
return typeof x === "function" || (typeof x === "object" && x !== null); | ||
} | ||
function resolve(thenable) { | ||
if (thenable instanceof Promise) { | ||
return thenable; | ||
} | ||
var promise = new Promise(function(resolve, reject) { | ||
var then; | ||
try { | ||
if ( objectOrFunction(thenable) ) { | ||
then = thenable.then; | ||
if (typeof then === "function") { | ||
then.call(thenable, resolve, reject); | ||
} else { | ||
resolve(thenable); | ||
} | ||
} else { | ||
resolve(thenable); | ||
} | ||
} catch(error) { | ||
reject(error); | ||
} | ||
return new Promise(function(resolve, reject) { | ||
resolve(thenable); | ||
}); | ||
return promise; | ||
} | ||
export { resolve }; |
{ | ||
"name": "rsvp", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "A lightweight library that provides tools for organizing asynchronous code", | ||
@@ -15,4 +15,5 @@ "main": "dist/commonjs/main.js", | ||
"grunt-browserify": "~1.0.2", | ||
"grunt-mocha-phantomjs": "~0.2.8", | ||
"grunt-mocha-test": "~0.4.0" | ||
"grunt-mocha-phantomjs": "~0.3.0", | ||
"grunt-mocha-test": "~0.5.0", | ||
"grunt-s3": "~0.2.0-alpha.2" | ||
}, | ||
@@ -19,0 +20,0 @@ "scripts": { |
@@ -11,2 +11,6 @@ # RSVP.js [![Build Status](https://secure.travis-ci.org/tildeio/rsvp.js.png?branch=master)](http://travis-ci.org/tildeio/rsvp.js) | ||
## downloads | ||
- [rsvp-latest](http://rsvpjs-builds.s3.amazonaws.com/rsvp-latest.js) | ||
## Promises | ||
@@ -13,0 +17,0 @@ |
/*global describe, specify, it, assert */ | ||
if (typeof Object.getPrototypeOf !== "function") { | ||
Object.getPrototypeOf = "".__proto__ === String.prototype | ||
? function (object) { | ||
return object.__proto__; | ||
} | ||
: function (object) { | ||
// May break if the constructor has been tampered with | ||
return object.constructor.prototype; | ||
}; | ||
} | ||
function objectEquals(obj1, obj2) { | ||
for (var i in obj1) { | ||
if (obj1.hasOwnProperty(i)) { | ||
if (!obj2.hasOwnProperty(i)) return false; | ||
if (obj1[i] != obj2[i]) return false; | ||
} | ||
} | ||
for (var i in obj2) { | ||
if (obj2.hasOwnProperty(i)) { | ||
if (!obj1.hasOwnProperty(i)) return false; | ||
if (obj1[i] != obj2[i]) return false; | ||
} | ||
} | ||
return true; | ||
} | ||
describe("RSVP extensions", function() { | ||
@@ -93,2 +121,38 @@ describe("self fulfillment", function(){ | ||
it('should not resolve multiple times', function(done) { | ||
var resolver, rejector, fulfilled = 0, rejected = 0; | ||
var thenable = { | ||
then: function(resolve, reject) { | ||
resolver = resolve; | ||
rejector = reject; | ||
} | ||
}; | ||
var promise = RSVP.Promise(function(resolve) { | ||
resolve(1); | ||
}); | ||
promise.then(function(value){ | ||
return thenable; | ||
}).then(function(value){ | ||
fulfilled++; | ||
}, function(reason) { | ||
rejected++; | ||
}); | ||
setTimeout(function() { | ||
resolver(1); | ||
resolver(1); | ||
rejector(1); | ||
rejector(1); | ||
setTimeout(function() { | ||
assert.equal(fulfilled, 1); | ||
assert.equal(rejected, 0); | ||
done(); | ||
}, 20); | ||
}, 20); | ||
}); | ||
describe('assimilation', function() { | ||
@@ -272,3 +336,3 @@ it('should assimilate if `resolve` is called with a fulfilled promise', function(done) { | ||
denodeifiedFunc(1, 2, 3).then(function() { | ||
assert.deepEqual(args, [1, 2, 3]); | ||
assert(objectEquals(args, [1, 2, 3])); | ||
done(); | ||
@@ -309,3 +373,3 @@ }); | ||
denodeifiedFunc(promise, thenable, nonPromise).then(function() { | ||
assert.deepEqual(args, [1, 2, 3]); | ||
assert(objectEquals(args, [1, 2, 3])); | ||
done(); | ||
@@ -336,3 +400,3 @@ }); | ||
denodeifiedFunc().then(function(value) { | ||
assert.deepEqual(value, [1, 2, 3]); | ||
assert(objectEquals(value, [1, 2, 3])); | ||
done(); | ||
@@ -393,3 +457,3 @@ }); | ||
denodeifiedWriteFile('dest.txt', denodeifiedReadFile('src.txt')).then(function () { | ||
assert.deepEqual(writtenTo, ['dest.txt', 'contents of src.txt']); | ||
assert(objectEquals(writtenTo, ['dest.txt', 'contents of src.txt'])); | ||
done(); | ||
@@ -467,3 +531,3 @@ }); | ||
RSVP.hash({}).then(function(results) { | ||
assert.deepEqual(results, {}); | ||
assert(objectEquals(results, {}), 'expected fulfillment'); | ||
done(); | ||
@@ -480,3 +544,3 @@ }); | ||
RSVP.hash({ promise: promise, syncThenable: syncThenable, asyncThenable: asyncThenable, nonPromise: nonPromise }).then(function(results) { | ||
assert.deepEqual(results, { promise: 1, syncThenable: 2, asyncThenable: 3, nonPromise: 4 }); | ||
assert(objectEquals(results, { promise: 1, syncThenable: 2, asyncThenable: 3, nonPromise: 4 })); | ||
done(); | ||
@@ -609,3 +673,3 @@ }); | ||
RSVP.all([promise, syncThenable, asyncThenable, nonPromise]).then(function(results) { | ||
assert.deepEqual(results, [1, 2, 3, 4]); | ||
assert(objectEquals(results, [1, 2, 3, 4])); | ||
done(); | ||
@@ -676,8 +740,2 @@ }); | ||
specify("it short circuits if RSVP.promise", function(){ | ||
var deferred = new RSVP.defer(); | ||
assert.equal(RSVP.resolve(deferred.promise), deferred.promise); | ||
}); | ||
describe("1. If x is a promise, adopt its state ", function(){ | ||
@@ -748,2 +806,5 @@ specify("1.1 If x is pending, promise must remain pending until x is fulfilled or rejected.", function(done){ | ||
// we likely don't need to test this, if the browser doesn't support it | ||
if (typeof Object.defineProperty !== "function") { done(); return; } | ||
Object.defineProperty(thenable, 'then', { | ||
@@ -776,2 +837,5 @@ get: function(){ | ||
// we likely don't need to test this, if the browser doesn't support it | ||
if (typeof Object.defineProperty !== "function") { done(); return; } | ||
Object.defineProperty(thenable, 'then', { | ||
@@ -962,1 +1026,55 @@ get: function(){ | ||
}); | ||
// thanks to @wizardwerdna for the test case -> https://github.com/tildeio/rsvp.js/issues/66 | ||
// Only run these tests in node (phantomjs cannot handle them) | ||
if (typeof module !== 'undefined' && module.exports) { | ||
describe("using reduce to sum integers using promises", function(){ | ||
var resolve = RSVP.resolve; | ||
it("should build the promise pipeline without error", function(){ | ||
var array, iters, pZero, i; | ||
array = []; | ||
iters = 1000; | ||
for (i=1; i<=iters; i++) { | ||
array.push(i); | ||
} | ||
pZero = resolve(0); | ||
array.reduce(function(promise, nextVal) { | ||
return promise.then(function(currentVal) { | ||
return resolve(currentVal + nextVal); | ||
}); | ||
}, pZero); | ||
}); | ||
it("should get correct answer without blowing the nextTick stack", function(done){ | ||
var pZero, array, iters, result, i; | ||
pZero = resolve(0); | ||
array = []; | ||
iters = 1000; | ||
for (i=1; i<=iters; i++) { | ||
array.push(i); | ||
} | ||
result = array.reduce(function(promise, nextVal) { | ||
return promise.then(function(currentVal) { | ||
return resolve(currentVal + nextVal); | ||
}); | ||
}, pZero); | ||
result.then(function(value){ | ||
assert.equal(value, (iters*(iters+1)/2)); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
} |
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
222487
44
7380
262
0
8
8