async.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { __awaiter } from "tslib";
  2. export function asyncWhile(checker, callback) {
  3. return __awaiter(this, void 0, void 0, function* () {
  4. let index = 0;
  5. const newCallback = () => __awaiter(this, void 0, void 0, function* () {
  6. const checkRes = yield checker(index);
  7. if (checkRes) {
  8. yield callback(index);
  9. index++;
  10. yield newCallback();
  11. }
  12. });
  13. return yield newCallback();
  14. });
  15. }
  16. export function asyncForEach(array, callback) {
  17. return __awaiter(this, void 0, void 0, function* () {
  18. let index = 0;
  19. const newCallback = () => __awaiter(this, void 0, void 0, function* () {
  20. const item = array[index];
  21. const res = yield callback(item, index);
  22. if (index < array.length - 1 && res !== false) {
  23. index++;
  24. yield newCallback();
  25. }
  26. });
  27. return yield newCallback();
  28. });
  29. }
  30. export function nextTick(time = 0) {
  31. return new Promise((resolve) => {
  32. setTimeout(() => {
  33. resolve(true);
  34. }, time);
  35. });
  36. }