-
Notifications
You must be signed in to change notification settings - Fork 725
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move specific promise resolver to a standalone service
- Loading branch information
1 parent
78b103e
commit b0afec0
Showing
9 changed files
with
62 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/*global define*/ | ||
define(function () { | ||
'use strict'; | ||
|
||
function PromisesResolver($q) { | ||
|
||
function allEvenFailed(promises) { | ||
if (!Array.isArray(promises)) { | ||
throw 'allEvenFailed can only handle an array of promises'; | ||
} | ||
var deferred = $q.defer(); | ||
if (promises.length === 0) { | ||
deferred.resolve([]); | ||
return deferred.promise; | ||
} | ||
|
||
var states = []; | ||
var results = []; | ||
|
||
promises.forEach(function (promise, key) { | ||
states[key] = false; // promises are not resolved by default | ||
}); | ||
|
||
promises.forEach(function (promise, key) { | ||
function resolve(result) { | ||
states[key] = true; | ||
results[key] = result; // result may be an error | ||
var allFinished = true; | ||
states.forEach(function (state) { | ||
if (!state) { | ||
allFinished = false; | ||
} | ||
}); | ||
if (allFinished) { | ||
deferred.resolve(results); | ||
} | ||
} | ||
// whether the promise ends with success or error, consider it done | ||
$q.when(promise).then(resolve, resolve); | ||
}); | ||
|
||
return deferred.promise; | ||
}; | ||
|
||
return { | ||
allEvenFailed: allEvenFailed | ||
}; | ||
} | ||
|
||
PromisesResolver.$inject = ['$q']; | ||
|
||
return PromisesResolver; | ||
}); |
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
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 was deleted.
Oops, something went wrong.