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.

[F] Convert each loops as asynchronous function

See original GitHub issue

šŸš€ Feature Proposal

It could be super to have each loops (for example eachCell or in my case eachRow) as an asynchronous funtionā€¦

Motivation

Iā€™m using KoaJs working with Promises and I try to make a script where everyRow should do a query to database ā€¦ But it do the job after I needed it ā€¦ Because eachRow is not an async function ā€¦

Example

For example I did :

let products = [];

worksheet.eachRow((row, rowNumber) => {
	if(rowNumber != 1) {
		let id = row.getCell('A');

		if(id) {
			let datas = await ProductManager.get(id);
			products.push(datas);
		}
		else {
			products.push({
				label: row.getCell('B'),
				price: row.getCell('C'),
			});
		}
	}
}

return ProductManager.save(products);

But as I said itā€™s not waiting the DB call to do the save function ā€¦ So for the moment I did a second loop like that :

let products = [];

worksheet.eachRow((row, rowNumber) => {
	if(rowNumber != 1) {
		products.push({
			id    : row.getCell('A'),
			label : row.getCell('B'),
			price : row.getCell('C'),
		});
	}
});

for(let i = 0, length = products.length; i < length; i++) {
	if(products[i].id) {
		let datas = await ProductManager.get(products[i].id);
		products[i] = datas;
	}
	else {
		products[i] = {
			label: products[i].label,
			price: products[i].price,
		};
	}
}

return ProductManager.save(products);

So it would be great if we could have a asyncEachRow like this :

await worksheet.asyncEachRow(async (row, rowNumber) => {

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
alubbecommented, Apr 15, 2020

Thank you for the feature proposal! I believe it coincides with something that Iā€™m working on for version 4.0.0 - async iteration (https://github.com/exceljs/exceljs/pull/1135). With it, you could just use this

const workbookReader = new ExcelJS.stream.xlsx.WorkbookReader('./file.xlsx');
for await (const worksheetReader of workbookReader) {
  for await (const row of worksheetReader) {
    // ...
  }
}

Would that fit for your use case?

2reactions
skineurcommented, Apr 13, 2020

Hello use async module

async.eachLimit(worksheet._rows, 1, async function(row) {
      const values = row.values;
      const response = await saveInDB(values);
    }, (err) => { 
      if(err) {
        console.log(err);
      }
      console.log('finnish')
})

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using async/await with a forEach loop - Stack Overflow
When evaluate for loop, we have await promise inside the async function, the execution will pause until the await promise is settled. So,...
Read more >
Asynchronous programming: futures, async, await | Dart
The await keyword works only in async functions. Here's an example that converts main() from a synchronous to asynchronous function. First, add the...
Read more >
JavaScript async and await in loops - freeCodeCamp
We need this async keyword because await is in the callback function). const forEachLoop = _ => { console.log(ā€œStartā€); fruitsToGet.forEach(Ā ...
Read more >
42 Asynchronous iteration - Exploring JS
The following asynchronous generator converts a synchronous iterable to an asynchronous iterable. It implements the function syncToAsyncIterable() that we haveĀ ...
Read more >
How to use async functions with Array.map in Javascript
The map function is easy to convert to async as the Promise.all built-in does the heavy lifting. But controlling concurrency requires someĀ ...
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