asyncUtil.js 623 B

1234567891011121314151617181920212223242526
  1. export function allPromiseFinish(promiseList) {
  2. var hasError = false;
  3. var count = promiseList.length;
  4. var results = [];
  5. if (!promiseList.length) {
  6. return Promise.resolve([]);
  7. }
  8. return new Promise(function (resolve, reject) {
  9. promiseList.forEach(function (promise, index) {
  10. promise.catch(function (e) {
  11. hasError = true;
  12. return e;
  13. }).then(function (result) {
  14. count -= 1;
  15. results[index] = result;
  16. if (count > 0) {
  17. return;
  18. }
  19. if (hasError) {
  20. reject(results);
  21. }
  22. resolve(results);
  23. });
  24. });
  25. });
  26. }