|
@@ -0,0 +1,54 @@
|
|
|
|
+const delay = (ms) => new Promise((resolve, reject) => setTimeout(() => resolve('Vika'), ms));
|
|
|
|
+function* likeAsync () {
|
|
|
|
+ console.log(1);
|
|
|
|
+ console.log(yield 1.5);
|
|
|
|
+ let res1 = yield delay(2000);
|
|
|
|
+ console.log(2, res1);
|
|
|
|
+ let res2 = yield delay(2000);
|
|
|
|
+ console.log(3, res2);
|
|
|
|
+ try {
|
|
|
|
+ console.log(yield fetch("http://localhost:10000"));
|
|
|
|
+ }
|
|
|
|
+ catch (e) {
|
|
|
|
+ console.log('generator error catch', e);
|
|
|
|
+ }
|
|
|
|
+ let res3 = yield delay(2000);
|
|
|
|
+ console.log(4, res3);
|
|
|
|
+
|
|
|
|
+ let res4 = yield delay(2000);
|
|
|
|
+ console.log(5, res4);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+function run (gena) {
|
|
|
|
+ let iter = gena();
|
|
|
|
+ function next(result, error) {
|
|
|
|
+ let value;
|
|
|
|
+ let done;
|
|
|
|
+
|
|
|
|
+ if (error) {
|
|
|
|
+ const errGen = iter.throw(error);
|
|
|
|
+ value = errGen.value;
|
|
|
|
+ done = errGen.done;
|
|
|
|
+ }
|
|
|
|
+ if(!error) {
|
|
|
|
+ const resultGen = iter.next(result);
|
|
|
|
+ value = resultGen.value;
|
|
|
|
+ done = resultGen.done;
|
|
|
|
+ }
|
|
|
|
+ if (done) {return};
|
|
|
|
+
|
|
|
|
+ if(value && typeof value?.then === 'function'){
|
|
|
|
+ value.then(next, err => next(undefined, err))
|
|
|
|
+ } else {
|
|
|
|
+ next(value);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ next();
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+run(likeAsync);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|