main.js 995 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. const delay = (ms) => new Promise(ok => setTimeout(() => ok(ms), ms));
  2. function run(gena) {
  3. let iter = gena();
  4. function next (result, error) {
  5. let value, done;
  6. if (!error) {
  7. [{value, done}] = [iter.next(result)];
  8. console.log( [{value, done}])
  9. } else {
  10. [{value, done}] = [iter.throw(result)];
  11. console.log( [{value, done}])
  12. }
  13. if(done){
  14. return;
  15. }
  16. if (value && typeof value?.then === 'function') {
  17. value.then(next, err => next(undefined, err));
  18. }else{
  19. next({value})
  20. }
  21. }
  22. next()
  23. }
  24. run(likeAsync);
  25. function* likeAsync() {
  26. console.log(1);
  27. console.log(yield 1.5);
  28. let res1 = yield delay(2000);
  29. console.log(2, res1);
  30. let res2 = yield delay(2000);
  31. console.log(3, res2);
  32. try {
  33. console.log(yield fetch('httpshghh://localhost:8080'));
  34. } catch (e) {
  35. console.log(e, 'Hi ERROR');
  36. }
  37. }