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 .keys(), .values() .entries() methods similar to the Map spec but instead they should return a Promise that resolves to an iterable.

Something like:

const keyv = new Keyv();
await keyv.set('foo', 'bar');
await keyv.set('fizz', 'buzz');

const keys = await keyv.keys();
for (const key of keys) {
  console.log(key);
}
// 'foo'
// 'fizz'

const values = await keyv.values();
for (const value of values) {
  console.log(value);
}
// 'bar'
// 'buzz'

const entries = await keyv.entries();
for (const [key, value] of entries) {
  console.log(`${key} => ${value}`);
}
// 'foo => bar'
// 'fizz => buzz'

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
ApexioDaCodercommented, Apr 12, 2022

This worked for me: keyv.opts.store.query('SELECT key FROM keyv;');

Edit: Changed from querying all to just the key. Thanks to HunteRoi.

3reactions
chocolateboycommented, Mar 8, 2019

they should return a Promise that resolves to an iterable.

This requires all of the values to be slurped (or slurpable) before each method resolves, potentially incurring a huge cost in time and memory. What if there are a million records in a remote backend — taking, say, a minute to retrieve, and chewing through a gig of (mobile) RAM — but the caller only needs to check the first two?

async function any (keyv, predicate) {
    const entries = await keyv.entries() // XXX uh-oh

    for (const [key, value] of entries) {
        if (predicate(key, value)) {
            return true
        }
    }
}

It would be more efficient to wait for a result, then process it, then wait for another result, then process that etc. The usual way to do this is not with a recordset (e.g. a promise which returns an array) but with a cursor (e.g. an async iterator):

async function any (keyv, predicate) {
    for await (const [key, value] of keyv.entries()) {
        if (predicate(key, value)) {
            return true
        }
    }
}

While consuming them in this way requires ES2018 (or Babel or TypeScript), producing them does not (just as any library can spit out promises for consumption by async/await). And, of course, the desugared usage is always available as a fallback:

async function any (keyv, predicate) {
    const it = keyv.entries()[Symbol.asyncIterator]()

    while (true) {
        const { done, value: pair } = await it.next()

        if (done) {
            return false
        }

        if (predicate(...pair)) {
            return true
        }
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Should Iterator or Iterable be used when exposing internal ...
I have a class with a private mutable list of data. I need to expose list items given following conditions: List should not...
Read more >
JavaScript Iterators and Iterables - AppSignal Blog
An iterable in JavaScript is an object that defines custom iteration behaviour via a method on itself or any of the objects in...
Read more >
Exposed/IterableEx.kt at master · JetBrains/Exposed - GitHub
Kotlin SQL Framework. Contribute to JetBrains/Exposed development by creating an account on GitHub. ... interface SizedIterable<out T> : Iterable<T> {.
Read more >
Iterators, Iterables and Itertools - Finxter
Now let's expose what's going on inside. First, let's look at the methods that l provides (the dir function lists the methods of...
Read more >
Demystifying ES6 Iterables & Iterators - freeCodeCamp
Let's demystify it. There are 2 core concepts: Iterable — described by a data structure that provides a way to expose its data...
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