[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:
- Created 3 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top 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 >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
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
Would that fit for your use case?
Hello use async module