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.

Given that the for await syntax is a thing now, I think it would be very useful if the .each() method returned an async iterator instead of using a classical callback.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
PaulKiddlecommented, Oct 9, 2020

This is difficult because the row callback for each is synchronous, but for await would require an aync callback (even if you don’t actually use any async methods in the for body). To achieve this result you’d have to keep each row in a queue and wait for the row callback to finish before emitting the next result.

Something like this should do it:

function resultQueue(){
  let returnValue;
  const q = [];
  return {
    getNext(){
      if(q.length){
        return q.shift();
      }
      return new Promise(resolve => {
        returnValue = v => {
          returnValue = null;
          resolve(v);
        };
      });
    },

    setNext(v){
      if(returnValue) {
        returnValue(v);
      } else {
        q.push(v);
      }
    }
  }
}

async function* asyncEach(statement) {
  const { getNext, setNext } = resultQueue();
  db.each(statement, setNext).then(() => setNext(null));
  let row;
  // Assuming there are no falsy values passed to `setNext` until the final callback
  // i.e. null return value indicates no more records
  while(row = await getNext()) {
    yield row;
  }
}

(async function(){
  for await(const row of asyncEach('SELECT * FROM table;')) {
    await doSomethingWithRow(row);
  }
}());
0reactions
brernetocommented, Jul 18, 2022

Why is there not a completed callback? An async queue is easy to do myself but I need to know when to end.

Edit: Sorry, just found the db property

Read more comments on GitHub >

github_iconTop Results From Across the Web

for await...of - JavaScript - MDN Web Docs
The for await...of statement creates a loop iterating over async iterable objects as well as sync iterables. This statement can only be used ......
Read more >
for await...of - JavaScript - UDN Web Docs: MDN Backup
The for await...of statement creates a loop iterating over async iterable objects as well as on sync iterables, including: built-in String , Array...
Read more >
Async/await - The Modern JavaScript Tutorial
The syntax: // works only inside async functions let value = await promise ...
Read more >
for await of VS Promise.all - javascript - Stack Overflow
for await is wrong, Promise.all is the only correct way to deal with this situation where you have multiple promises that you need...
Read more >
for await loops (Part I)
A feature of other languages - like JavaScript for example - and of the macro-based futures-async-await prototype is a syntax for writing for ......
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