Last active
March 16, 2020 10:43
-
-
Save SugarDarius/5a0ce9189980c0f21012c50cc2c5afd6 to your computer and use it in GitHub Desktop.
Promise Chain base on Axios which merge promises into on single response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Promise chain | |
* author: Aurélien Dupays Dexemple | |
*/ | |
import { AxiosStatic, AxiosPromise, AxiosResponse } from 'axios'; | |
export const PromiseChain = (axios: AxiosStatic, promises: AxiosPromise[]): Promise<AxiosResponse<any>> => ( | |
axios.all(promises).then((responses: AxiosResponse[]): AxiosResponse<any> => ( | |
responses.reduce(( finalResponse: AxiosResponse, { data, ...rest }: AxiosResponse) => ({ | |
...rest, | |
data: { ...data, ...finalResponse.data } | |
})) | |
) | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment