12345678910111213141516171819202122232425262728293031323334353637383940 |
- const delay = (ms) => new Promise(ok => setTimeout(() => ok(ms), ms));
- function run(gena) {
- let iter = gena();
- function next (result, error) {
- let value, done;
- if (!error) {
- [{value, done}] = [iter.next(result)];
- console.log( [{value, done}])
- } else {
- [{value, done}] = [iter.throw(result)];
- console.log( [{value, done}])
- }
- if(done){
- return;
- }
- if (value && typeof value?.then === 'function') {
- value.then(next, err => next(undefined, err));
- }else{
- next({value})
- }
- }
- next()
- }
- run(likeAsync);
- 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('httpshghh://localhost:8080'));
- } catch (e) {
- console.log(e, 'Hi ERROR');
- }
- }
|