A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/AsyncGenerator/next below:

AsyncGenerator.prototype.next() - JavaScript | MDN

AsyncGenerator.prototype.next()

Baseline Widely available

next() メソッドは、シーケンス内の次の値を返します。

構文
asyncGeneratorObject.next()
asyncGeneratorObject.next(value)
引数
value 省略可

ジェネレーターの内部状態を変更するために使用するオプションの値。next() メソッドに渡された値は、yield で受け取ります。

返値

Promise で、2 つのプロパティを持つ Object に解決します。

done

論理値です。

value

ジェネレーターによってもたらされる、または返される任意の JavaScript 値。

例 next() の使用

次の例は、単純なジェネレーターと next メソッドが返すオブジェクトを示しています。

// 非同期タスクです。実際にはもっと有益なことを使用していることを
// 想定してください。
function delayedValue(time, value) {
  return new Promise((resolve, reject) => {
    setTimeout(() => resolve(value), time);
  });
}

async function* createAsyncGenerator() {
  yield delayedValue(500, 1);
  yield delayedValue(500, 2);
  yield delayedValue(500, 3);
}

const asyncGen = createAsyncGenerator();
asyncGen.next().then((res) => console.log(res)); // { value: 1, done: false }
asyncGen.next().then((res) => console.log(res)); // { value: 2, done: false }
asyncGen.next().then((res) => console.log(res)); // { value: 3, done: false }
asyncGen.next().then((res) => console.log(res)); // { value: undefined, done: true }
ジェネレーターに値を送信

この例では、next が値付きで呼び出されます。

メモ: 最初の呼び出しは、ジェネレーターが最初は何も出力しなかったため、何もログ出力しません。

// 非同期タスクです。実際にはもっと有益なことを使用していることを
// 想定してください。
function sleep(time) {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, time);
  });
}

async function* createAsyncGenerator() {
  while (true) {
    await sleep(500);
    const value = yield;
    console.log(value);
  }
}

async function main() {
  const asyncGen = createAsyncGenerator();
  // No log at this step: the first value sent through `next` is lost
  console.log(await asyncGen.next(1)); // { value: undefined, done: false }
  // Logs 2: the value sent through `next`
  console.log(await asyncGen.next(2)); // { value: undefined, done: false }
}

main();
仕様書 ブラウザーの互換性 関連情報

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4