lifeography
4/23/2018 - 6:24 AM

async/await - Process array in parallel

We can slightly change the code to run async operations in parallel. This code will run many “delayLog” tasks in parallel. But be careful with very large array (too many tasks in parallel can be too heavy for CPU or memory).

async function processArray(array) {
	// map array to promises
	const promises = array.map(delayedLog);
	// wait until all promises are resolved
	await Promise.all(promises)
	console.log('Done!');
}