|
@@ -0,0 +1,29 @@
|
|
|
|
+const delay = ms => new Promise((ok, neOk) => setTimeout(() => ok(ms), ms))
|
|
|
|
+
|
|
|
|
+function run(gena) {
|
|
|
|
+ let iter = gena()
|
|
|
|
+ function next(promiseResult, error=false) {
|
|
|
|
+ let a
|
|
|
|
+ if (!error) {
|
|
|
|
+ a = iter.next(promiseResult)
|
|
|
|
+ } else {
|
|
|
|
+ iter.throw(promiseResult)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (a.value instanceof Promise) {
|
|
|
|
+ a.value.then(result => next(result)).catch(e => next(e, true))
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ next()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function* check() {
|
|
|
|
+ console.log(1)
|
|
|
|
+ let result1 = yield delay(1000)
|
|
|
|
+ console.log(result1)
|
|
|
|
+ let result2 = yield delay(2000)
|
|
|
|
+ console.log(result2)
|
|
|
|
+ return 100500
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+run(check)
|