Array.fromAsync & Grouping in JavaScript
From the JavaScript Array Methods cheat sheet · Modern Array Methods (ES2023+) · verified Jul 2026
Array.fromAsync & Grouping
Build arrays from async iterables, group by key (Object.groupBy / Map.groupBy)
javascript
// Array.fromAsync — collect an async iterable into an array
async function* lines(file) {
for await (const chunk of file.stream()) yield chunk
}
const allLines = await Array.fromAsync(lines(myFile))
// Object.groupBy — group items by a derived key (ES2024)
const items = [
{ name: 'apple', type: 'fruit' },
{ name: 'carrot', type: 'veg' },
{ name: 'pear', type: 'fruit' },
]
const byType = Object.groupBy(items, x => x.type)
// { fruit: [{...apple}, {...pear}], veg: [{...carrot}] }
// Map.groupBy — same idea, but the key can be any value (incl. objects)
const byParent = Map.groupBy(rows, r => r.parent)💡 Array.fromAsync replaces the manual `for await ... push` loop
⚡ Object.groupBy returns a null-prototype object — no .hasOwnProperty needed
📌 Map.groupBy is the version to use when keys are objects, not strings
🎯 Pair fromAsync with fetch + ReadableStream for clean streaming-to-array code
es2024asyncgrouping