1234567891011121314151617181920212223242526272829303132333435363738394041 |
- function* likeFor(end, step = 1, start = 0) {
- for (let i = start; i < end; i += step) {
- yield i;
- }
- }
- function* twice() {
- yield* gena(10);
- yield* gena(20);
- }
- const delay = (ms) => new Promise((ok) => setTimeout(() => ok(ms), ms));
- function* likeAsync() {
- console.log(1);
- let res1 = yield delay(2000);
- console.log(2, res1);
- let res2 = yield delay(2000);
- console.log(3, res2);
- }
- function run(likeAsync) {
- let iter = likeAsync();
- function next(result, err) {
- let value, done;
- if (!err) {
- [{ value, done }] = [iter.next(result)];
- } else {
- [{ value, done }] = [iter.throw(err)];
- }
- if (done) return;
- if (value && typeof value?.then === "function") {
- value.then(next, (err) => next(undefined, err));
- } else {
- next(value);
- }
- }
- next();
- }
- run(likeAsync);
|