Re: [whatwg/streams] ReadableStream.from(asyncIterable) (#1083)

@MattiasBuelens commented on this pull request.



> +  }
+  if (typeof func !== 'function') {
+    throw new TypeError();
+  }
+  return func;
+};
+
+exports.GetIterator = (obj, hint = 'sync', method) => {
+  assert(hint === 'sync' || hint === 'async');
+  if (method === undefined) {
+    if (hint === 'async') {
+      method = exports.GetMethod(obj, Symbol.asyncIterator);
+      if (method === undefined) {
+        const syncMethod = exports.GetMethod(obj, Symbol.iterator);
+        const syncIterator = exports.GetIterator(obj, 'sync', syncMethod);
+        return syncIterator; // TODO sync to async iterator

I made an implementation for [`CreateAsyncFromSyncIterator`](https://tc39.es/ecma262/#sec-createasyncfromsynciterator)... but I don't know how I feel about it. 😅
```javascript
function CreateAsyncFromSyncIterator(iteratorRecord) {
  const syncIterator = {
    [Symbol.iterator]: () => iteratorRecord.iterator
  };
  const asyncIterator = (async function* () {
    return yield* syncIterator;
  }());
  const nextMethod = asyncIterator.next;
  return { iterator: asyncIterator, nextMethod, done: false };
}
```
Basically, I use an async generator function that delegates to the given sync iterator (with `yield*`), and use that as the async iterator.

It *works*, but maybe it's too confusing or distracting for the *reference* implementation? 🤷 We could also leave it as it is now, with a comment explaining that the reference implementation doesn't really care if `GetIterator()` returns a sync or an async iterator.

-- 
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/pull/1083#discussion_r525578235

Received on Tuesday, 17 November 2020 22:51:54 UTC