Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

Proof of concept: asyncIterator #338

Closed
wants to merge 1 commit into from
Closed

Proof of concept: asyncIterator #338

wants to merge 1 commit into from

Conversation

vweevers
Copy link
Member

@vweevers vweevers commented Apr 27, 2019

Do not merge. Ignore failing tests.

This is to find out what we need to support Symbol.asyncIterator in userland (we cannot do it here because we need async generators to properly handle ending on errors, and async generators are only available in Node 10+ and Chrome).

The answer is that we need to add Promise support to iterator.next() and .end(). We can do that in the same way as levelup: if you don't provide a callback argument, you get back a Promise.

This means there will be three ways to iterate:

  1. callbacks
  2. async/await (a bit awkward)
const it = db.iterator()
const kv = await it.next()

if (!kv) {
  await it.end()
} else {
  const [key, value] = kv
}
  1. for await...of
for await (let [key, value] of db.iterator()) {
  // no need to call end
}

cc @chjj @peakji


Previous discussions:

@vweevers
Copy link
Member Author

Ah. Implementations like memdown that don't need an explicit end() call can implement asyncIterator themselves without an async generator, thus avoiding syntax errors in older runtimes. The level-js iterator currently does need an explicit end() but that will change with Level/level-js#86.

So we don't have to do it in userland. The strategy is:

  • abstract-leveldown implements Symbol.asyncIterator with an async generator, lazily required (only in node 10+) and excluded from browserify/webpack builds
  • memdown (and later level-js) implements Symbol.asyncIterator without async generator

vweevers added a commit that referenced this pull request Sep 12, 2021
Closes #235.

Supersedes #338, which was just a proof of concept and we've since
dropped support of legacy runtimes (Level/community#98) which now
allows us to use async generators across the board.
@vweevers
Copy link
Member Author

Superseded by #379.

@vweevers vweevers closed this Sep 12, 2021
@vweevers vweevers deleted the async-iterator branch September 12, 2021 17:07
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant