Array.fromAsync() is a new Javascript method introduced in ECMAScript 2023. It allows arrays to be created from sources that are asynchronous. Array.fromAsync() works by waiting for all the promises to resolve before returning a new array with the values that have been resolved.
Structure
Array.fromAsync(array);
Array.fromAsync(array, mapFn);
Array.fromAsync(array, mapFn, arg);
array: An array or iterable data structure.
mapFn: A function that can be invoked on every array element.
arg: Value to use when mapFn is invoked.
Example
const promises = [
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3)
];
Array.fromAsync(promises).then((array) => console.log(array)); //[1, 2, 3]