-
Notifications
You must be signed in to change notification settings - Fork 723
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b68168a
commit 307d042
Showing
9 changed files
with
60 additions
and
64 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 was deleted.
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
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
48 changes: 48 additions & 0 deletions
48
src/javascripts/ng-admin/es6/lib/Utils/PromisesResolver.js
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,48 @@ | ||
|
||
class PromisesResolver { | ||
static allEvenFailed(promises) { | ||
if (!Array.isArray(promises)) { | ||
throw 'allEvenFailed can only handle an array of promises'; | ||
} | ||
|
||
return new Promise(function (resolve, reject) { | ||
if (promises.length === 0) { | ||
return resolve([]); | ||
} | ||
|
||
let states = [], | ||
results = []; | ||
|
||
promises.forEach((promise, key) => { | ||
states[key] = false; // promises are not resolved by default | ||
}); | ||
|
||
promises.forEach((promise, key) => { | ||
function resolveState(result) { | ||
states[key] = true; | ||
results[key] = result; // result may be an error | ||
for (let i in states) { | ||
if (!states[i]) { | ||
return; | ||
} | ||
} | ||
|
||
resolve(results); | ||
} | ||
|
||
function resolveSuccess(result) { | ||
return resolveState({status: 'success', result: result}); | ||
} | ||
|
||
function resolveError(result) { | ||
return resolveState({status: 'error', error: result}) | ||
} | ||
|
||
// whether the promise ends with success or error, consider it done | ||
promise.then(resolveSuccess, resolveError); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
export default PromisesResolver; |