Skip to content

Instantly share code, notes, and snippets.

@YNSTakeru
Created March 17, 2024 19:41
Show Gist options
  • Save YNSTakeru/8d3386f69b13c77327d7494c7afb35f7 to your computer and use it in GitHub Desktop.
Save YNSTakeru/8d3386f69b13c77327d7494c7afb35f7 to your computer and use it in GitHub Desktop.

問題2 ヒント

クリックして開く
  • Generator Functionfor ofを使ってもnext()のような挙動をとります。
  • Generator Function内でreturnするとGenerator Functionが機能しなくなります。

問題2 解答

クリックして開く
function delay(ms, value) {
  return new Promise((resolve) => setTimeout(() => resolve(value), ms));
}

async function* delayGenerator() {
  yield await delay(2000, "開始");
  yield await delay(1000, "中間");
  yield await delay(1000, "終わり");
  return;
}

async function main() {
  const delayGen = delayGenerator();
  for await (const value of delayGen) {
    console.log(value);
  }
}

main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment