Re: [whatwg/streams] ReadableStream should be an async iterable (#778)

@jakearchibald Getting two errors at jsbin 

`Uncaught SyntaxError: Unexpected token *` 

when both `async` and `*` are used, and

`Uncaught SyntaxError: Unexpected reserved word`

at `await` before `for..of` loop at Chromium 60 with `--js-flags=--harmony-async-iteration` flag set.

Can you provide link documentations relevant to `--js-flags=--harmony-async-iteration` flag, which does not appear to be listed at [List of Chromium Command Line Switches](https://peter.sh/experiments/chromium-command-line-switches/)?

`await` can be used both before `iterable` at `(variable of iterable)` and within `for..of` loop `statement`. If expected result is for iteration to wait for current `Promise` value following `await`. For example


```
// wait for each `iterable` before reaching `statement`
(async() => {
  for (let prop of [await new Promise(resolve => 
      setTimeout(resolve, Math.floor(Math.random() * 10000), 1)), await new Promise(resolve => 
      setTimeout(resolve, Math.floor(Math.random() * 5000), 2))]
      ) {
    console.log(prop);
  };
 })();
```

```
// use generator function for `iterable`
(async(n) => {
  function* gen() {
    while(++n < 5) yield new Promise(resolve => setTimeout(resolve, 1000, {value:n, done:false}))
  }

  for (let prop of gen()) {
    console.log(await prop)
  }
})(0);
```

```
// pass an array of functions to be called within `statement`,
// next `iterable` awaits fulfillment of `Promise` following `await` 
const fn = async(res, ...props) => {
  for (let prop of props) {
    res.push(await prop());
    console.log(res);
  };
  return res
}

const arr = [
  () =>
    new Promise((resolve, reject) => 
      setTimeout(() =>
        resolve(0)
      , Math.floor(Math.random() * 2000))
    ),
  () => 
    new Promise((resolve, reject) => 
      setTimeout(() => 
        resolve(1) 
      , Math.floor(Math.random() * 2000))
    )
  ];

fn([], ...arr)
.then(data => console.log("done:", data))
.catch(err => console.error(err));
```



Adjusted pattern at linked jsbin https://jsbin.com/zusexeripu/edit?js,console to not combine `async` with `*` at function declaration for number example

```
function* asyncRandomNumbers() {
  // This is a web service that returns a random number
  const url = 'https://www.random.org/decimal-fractions/?num=1&dec=10&col=1&format=plain&rnd=new';
  while(true) yield fetch(url).then(response => response.text()).then(text => Number(text));  
}
async function example() {
  for (const p of asyncRandomNumbers()) {
    const n = await p;
    
    if (n > 0.95) {
      console.log("done", n);
      break;
    } else {
        console.log(n);
    }
  }
}

example();
```

To iterate a `ReadableStream` we can use several different patterns, depending on expected flow control and result

```
async function readStream() {
  let n = 0;
  const rs = new ReadableStream({
    pull(c) {
      if (n++ < 5) {
        c.enqueue(n)
      } else {
        c.close()
      }
    }
  });
  const reader = rs.getReader();
  const res = [];
  async function processStream() {
    return await reader.read();
  }

  function* gen() {
    while(true) yield processStream()
  }
  
  for (let stream of gen()) {
    const {value, done} = await stream; 
    if (done) break;
    res.push(value);
    console.log(value, done);
  }

  return {res, rs, reader, complete: await reader.closed.then(() => true)};
  
};

```


-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/778#issuecomment-325225833

Received on Sunday, 27 August 2017 21:26:23 UTC