Add iterator(options) and return [Symbol.asyncIterator]
See original GitHub issueI’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:
- Created 6 years ago
- Comments:22 (22 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
Breaking this down:
levelup#iterator
: #586iterator#seek
: Level/abstract-leveldown#234levelup#iterator
anditerator#seek
(in userland to start) (#491)[Symbol.asyncIterator]
to abstract iterator: Level/abstract-leveldown#235[Symbol.asyncIterator]
to streams: will land soon inreadable-stream
.