1.0.3 • Published 2 years ago
promises-to-array v1.0.3
PromiseArray
Change array of promises to promise of array. Works out-of-the-box with Node, React and Angular.
It should also work with anything else, as it doesn't rely on any library.
Usage
Install from NPM:
npm i promises-to-array
import { ArrayOfPromisesToPromiseArray } from "promises-to-array"
// Assumes yourPromises is an array of Promises<AnyType>, for example
// from making many fetch requests
const promiseOfArray=ArrayOfPromisesToPromiseArray(yourPromises)
Example
import { ArrayOfPromisesToPromiseArray } from "promises-to-array"
const one: Promise<number> = new Promise((resolve, reject) => { resolve(1) })
const two: Promise<number> = new Promise((resolve, reject) => { resolve(2) })
const three: Promise<number> = new Promise((resolve, reject) => { resolve(3) })
const four: Promise<number> = new Promise((resolve, reject) => { resolve(4) })
const five: Promise<number> = new Promise((resolve, reject) => { resolve(5) })
const testingArray: Array<Promise<number>> = [
one, two, three,
four, five
]
ArrayOfPromisesToPromiseArray<number>(testingArray)
.then(wholeArray => {
// Do whatever with regular .map , .filter ...
console.log(wholeArray.filter(x => x > 3))
})