close
The Wayback Machine - https://web.archive.org/web/20201015042610/https://github.com/sisterAn/JavaScript-Algorithms/issues/56
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

阿里&字节:手写 async/await 的实现 #56

Open
sisterAn opened this issue May 28, 2020 · 2 comments
Open

阿里&字节:手写 async/await 的实现 #56

sisterAn opened this issue May 28, 2020 · 2 comments
Labels

Comments

@sisterAn
Copy link
Owner

@sisterAn sisterAn commented May 28, 2020

No description provided.

@0undefined0
Copy link

@0undefined0 0undefined0 commented May 29, 2020

之前看过一篇不错的,看看顺便默写下代码

function asyncToGen(genFunction) {
  return function (...args) {
    const gen = genFunction.apply(this, args);
    return new Promise((resolve, reject) => {
      function step(key, arg) {
        let genResult;
        try {
          genResult = gen[key](arg);
        } catch (err) {
          return reject(err);
        }
        const { value, done } = genResult;
        if (done) {
          return resolve(value);
        }
        return Promise.resolve(value).then(
          (val) => {
            step('next', val);
          },
          (err) => {
            step('throw', err);
          },
        );
      }
      step('next');
    });
  };
}
const getData = () => new Promise(resolve => setTimeout(() => resolve('data'), 1000));
function* testG() {
  const data = yield getData();
  console.log('data: ', data);
  const data2 = yield getData();
  console.log('data2: ', data2);
  return 'success';
}

const gen = asyncToGen(testG);
gen().then(res => console.log(res));
@tjwyz
Copy link

@tjwyz tjwyz commented Jul 16, 2020

本质是希望实现一个co函数

let delay = function (time, fnc) {
	setTimeout(() => {
		fnc(time);
	}, time);
}

let promisefy = (fn) => {
	return (...arg) => {
		return new Promise ((resolve, reject)=> {
			fn(...arg, (param)=>{
				resolve(param);
			})
		});
	}
}

let delayP = promisefy(delay);

const gen = function* () {
	const ret1 = yield delayP(1000);
	console.log(ret1);
	const ret2 = yield delayP(2000);
	console.log(ret2);
}

// 阴间写法
const g = gen();
g.next().value.then((res1)=>{
	g.next(res1).value.then((res2)=>{
		//
	});
})


// 正常写法
function co (generator) {
	return new Promise((resolve, reject)=>{
		const gen = generator();
		function next (...param) {
			let tmp = gen.next(...param);
			if (tmp.done) {
				resolve(tmp.value);
				return;
			}
			tmp.value.then((...ret)=>{
				next(...ret);
			})
		}
		next();
	})
}

co(gen).then((res)=>{
    console.log(res);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.