Skip to content

[F] Convert each loops as asynchronous function #1212

Closed
@trenyture

Description

@trenyture

🚀 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) => {

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy