question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add iterator(options) and return [Symbol.asyncIterator]

See original GitHub issue

I’d like to propose adding a method to levelup that would allow direct iteration of the underlying store.iterator. This would negate the need for consumers to convert the ReadStream to an Iterable.

from a usage perspective, something along the lines of:

async function main(){
  let db = levelup(encode(leveldown()));

  await db.put("foo", "hello");
  await db.put("bar", "world");

  let search = db.read( { leq: "bar", limit: 10 });

  for await (let data of search){
    let key = data.key;
    let value = data.value;
  }
}

not a huge change to implement this:

LevelUP.prototype.read= function (options) {
  options = extend({ keys: true, values: true }, options)
  if (typeof options.limit !== 'number') { options.limit = -1 }
  return new LevelAsyncIterator(this.db.iterator(options), options)
}

where LevelAsyncIterator can be the stream iterator butchered to remove the Stream aspects and simply implement [Symbol.asyncIterator] instead - along with ability to end (clean) the underlying store.iterator

If people are happy with this proposal I can make the PRs and a new level-iterator repo, based off level-stream-iterator?

I’ll do it as a plugin to levelup due to Symbol.asyncIterator being still stage-3.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:22 (22 by maintainers)

github_iconTop GitHub Comments

3reactions
MeirionHughescommented, Sep 20, 2017

Looks like async iteration is going stable - it is shipped in V8 - so hopefully we’ll see it without flags in node 9 https://github.com/tc39/proposal-async-iteration/issues/63#issuecomment-330978069

1reaction
vweeverscommented, Jun 16, 2018

Breaking this down:

  • Add levelup#iterator: #586
  • Add iterator#seek: Level/abstract-leveldown#234
  • Implement multiget using levelup#iterator and iterator#seek (in userland to start) (#491)
  • Add [Symbol.asyncIterator] to abstract iterator: Level/abstract-leveldown#235
  • Add [Symbol.asyncIterator] to streams: will land soon in readable-stream.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Symbol.asyncIterator - JavaScript - MDN Web Docs
The Symbol.asyncIterator well-known symbol specifies the default async iterator for an object. If this property is set on an object, ...
Read more >
Async iteration and generators - The Modern JavaScript Tutorial
Use Symbol.asyncIterator instead of Symbol.iterator . The next() method should return a promise (to be fulfilled with the next value).
Read more >
Using javascript's Symbol.asyncIterator with for await of loop
I create an empty object a and insert a key Symbol.asyncIterator on the same object and assign it a function named test that...
Read more >
JavaScript async iterators - Node.js Design Patterns
The main difference is that this type we have to use Symbol.asyncIterator and that it must return an async iterator.
Read more >
Async iterators and generators - JakeArchibald.com
Whereas the for-await loop will get its iterator by calling asyncThing[Symbol.asyncIterator] if it's defined, otherwise it will fall back to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found