From 2e2792e3894aaf92e324a1921a1200a7d34daffb Mon Sep 17 00:00:00 2001 From: Shaun Lebron Date: Mon, 11 Jun 2018 09:46:40 -0500 Subject: [PATCH 1/2] add for-await-of equivalence example --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index eb6cce0..eea4ee8 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,15 @@ for await (const line of readLines(filePath)) { } ``` +This is equivalent to: + +```js +for (const _ of readLines(filePath)) { + const line = await _; + console.log(line); +} +``` + Async for-of statements are only allowed within async functions and async generator functions (see below for the latter). During execution, an async iterator is created from the data source using the `[Symbol.asyncIterator]()` method. From 5e59b1cc265949522948a175003eddc9f28f1b45 Mon Sep 17 00:00:00 2001 From: Shaun Lebron Date: Mon, 11 Jun 2018 16:31:15 -0500 Subject: [PATCH 2/2] correct equivalent example --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index eea4ee8..9abf4e2 100644 --- a/README.md +++ b/README.md @@ -37,9 +37,11 @@ for await (const line of readLines(filePath)) { This is equivalent to: ```js -for (const _ of readLines(filePath)) { - const line = await _; - console.log(line); +const i = readLines(filePath); +while (true) { + const {value, done} = await i.next(); + if (done) break; + console.log(value); } ```