-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use proper promise idiom in Provider
- Loading branch information
Showing
9 changed files
with
172 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict' | ||
describe 'Promises', -> | ||
describe 'any', -> | ||
it 'resolves single object', (done) -> | ||
p = Promises.any 'Foo' | ||
p.then (value) -> | ||
expect(value).toBe 'Foo' | ||
done() | ||
|
||
it 'rejects single promise', (done) -> | ||
p = Promises.any Promise.reject 'Bar' | ||
p.catch (value) -> | ||
expect(value).toBe 'Bar' | ||
done() | ||
|
||
it 'resolves empty', (done) -> | ||
p = Promises.any [] | ||
p.catch (e) -> | ||
expect(e).toBeFalsy() | ||
done() | ||
|
||
it 'falls back', (done) -> | ||
p = Promises.any [ | ||
Promise.reject 'A' | ||
Promise.reject 'B' | ||
Promise.resolve 'C' | ||
] | ||
p.then (value) -> | ||
expect(value).toBe 'C' | ||
done() | ||
|
||
it 'resolves on first', (done) -> | ||
p = Promises.any [ | ||
Promise.reject 'A' | ||
Promise.resolve 'B' | ||
Promise.resolve 'C' | ||
] | ||
p.then (value) -> | ||
expect(value).toBe 'B' | ||
done() | ||
|
||
it 'rejects if none resolve', (done) -> | ||
p = Promises.any [ | ||
Promise.reject 'A' | ||
Promise.reject 'B' | ||
Promise.reject 'C' | ||
] | ||
p.catch (errors) -> | ||
expect(errors).toEqual ['A', 'B', 'C'] | ||
done() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Promises extra functionality | ||
* @license MIT | ||
* @author Jim Chen | ||
*/ | ||
var Promises = (function( ) { | ||
|
||
var Promises = {}; | ||
|
||
/** | ||
* Resolves as soon as any promise resolves in the order of the input array | ||
* | ||
* @param arr - array of promises | ||
* @return promise that resolves if any one promise resolves and rejects | ||
* if otherwise | ||
**/ | ||
Promises.any = function (promises) { | ||
if (!Array.isArray(promises)) { | ||
// Is raw object or promise, resolve it directly | ||
return Promise.resolve(promises); | ||
} | ||
if (promises.length === 0) { | ||
// No promises to resolve so we think it failed | ||
return Promise.reject(); | ||
} | ||
return new Promise(function (resolve, reject) { | ||
var hasResolved = false; | ||
var hasCompleted = 0; | ||
var errors = []; | ||
for (var i = 0; i < promises.length; i++) { | ||
promises[i].then(function (value) { | ||
hasCompleted++; | ||
if (!hasResolved) { | ||
hasResolved = true; | ||
resolve(value); | ||
} | ||
}).catch((function (i) { | ||
return function (e) { | ||
hasCompleted++; | ||
errors[i] = e; | ||
if (hasCompleted === promises.length) { | ||
// All promises have completed and we are in rejecting case | ||
if (!hasResolved) { | ||
reject(errors); | ||
} | ||
} | ||
} | ||
})(i)); | ||
} | ||
}); | ||
}; | ||
|
||
return Promises; | ||
})(); |