script.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  2. function* likeAsync() {
  3. console.log(1)
  4. console.log(yield 1.5)
  5. let res1 = yield delay(2000)
  6. console.log(2, res1)
  7. let res2 = yield delay(2000)
  8. console.log(3, res2)
  9. try {
  10. console.log(yield fetch("htpsdasd://localhost:10000"))
  11. } catch (e) {
  12. console.log('GENA ERROR CATCH', e)
  13. }
  14. let res3 = yield delay(3000)
  15. console.log(4, res3)
  16. }
  17. let likeA = likeAsync()
  18. console.log(likeA.next())
  19. console.log(likeA.next().value.then(res => console.log(res)))
  20. function run(gena) {
  21. let iter = gena()
  22. function next(res, err) {
  23. let value, done
  24. try {
  25. if(!err) {
  26. //литеральная деструктуризация???
  27. [{value, done}] = [iter.next(res)]
  28. } else {
  29. [{value, done}] = [iter.throw(err)]
  30. }
  31. if (done) return;
  32. if(value && typeof value?.then === 'function') {
  33. value.then(next, err => next(undefined, err))
  34. } else if (value){
  35. next(value)
  36. }
  37. } catch(e) {
  38. console.warn('gena error', e)
  39. }
  40. }
  41. next()
  42. }