Skip to content

Instantly share code, notes, and snippets.

@SugarDarius
Last active March 16, 2020 10:43
Show Gist options
  • Save SugarDarius/5a0ce9189980c0f21012c50cc2c5afd6 to your computer and use it in GitHub Desktop.
Save SugarDarius/5a0ce9189980c0f21012c50cc2c5afd6 to your computer and use it in GitHub Desktop.
Promise Chain base on Axios which merge promises into on single response
/*
* 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