Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Promise All with Axios

I just read an Article related to promise and was unable to comprehend how we can do multiple API call using Axios via Promise.all

So consider there are 3 URL, lets call it something like this

let URL1 = "https://www.something.com" let URL2 = "https://www.something1.com" let URL3 = "https://www.something2.com" 

And an array in which we will store Value

  let promiseArray = [] 

Now, I want to run this in parallel (Promise.all), but I am unable to figure our how will we do it? Because axios have a promise in itself (or at-least that's how I have used it).

axios.get(URL).then((response) => { }).catch((error) => { }) 

Question: Can someone please tell me how we can we send multiple request using promise.all and axios

like image 312
anny123 Avatar asked Oct 05 '18 16:10

anny123


People also ask

What is the use of Promise all ()?

The Promise. all() method takes an iterable of promises as an input, and returns a single Promise that resolves to an array of the results of the input promises. This returned promise will fulfill when all of the input's promises have fulfilled, or if the input iterable contains no promises.

What is Axios all?

axios. all is a helper method built into Axios to deal with concurrent requests. Instead of making multiple HTTP requests individually, the axios. all method allows us to make multiple HTTP requests to our endpoints altogether.

How Use Promise all with async await?

Inside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.

What is Axios Promise?

Axios is a modern, Promise-based HTTP client library. This means that Axios is used to send an HTTP request and handle their responses, all using JavaScript's promises. Axios supports both Node. js and JavaScript in the browser. Axios is also free and open-source.


2 Answers

The axios.get() method will return a promise.

The Promise.all() requires an array of promises. For example:

Promise.all([promise1, promise2, promise3]) 

Well then...

let URL1 = "https://www.something.com" let URL2 = "https://www.something1.com" let URL3 = "https://www.something2.com"  const promise1 = axios.get(URL1); const promise2 = axios.get(URL2); const promise3 = axios.get(URL3);  Promise.all([promise1, promise2, promise3]).then(function(values) {   console.log(values); }); 

You might wonder how the response value of Promise.all() looks like. Well then, you could easily figure it out yourself by taking a quick look at this example:

var promise1 = Promise.resolve(3); var promise2 = 42; var promise3 = new Promise(function(resolve, reject) {   setTimeout(resolve, 100, 'foo'); });  Promise.all([promise1, promise2, promise3]).then(function(values) {   console.log(values); }); // expected output: Array [3, 42, "foo"] 

For more information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

like image 177
You Nguyen Avatar answered Sep 24 '22 23:09

You Nguyen


fetchData(URL) function makes a network request and returns promise object with pending status.

Promise.all will wait till all promises are resolved or any promise is rejected. It returns a promise and resolve with array of responses.

let URLs= ["https://jsonplaceholder.typicode.com/posts/1", "https://jsonplaceholder.typicode.com/posts/2", "https://jsonplaceholder.typicode.com/posts/3"]  function getAllData(URLs){   return Promise.all(URLs.map(fetchData)); }  function fetchData(URL) {   return axios     .get(URL)     .then(function(response) {       return {         success: true,         data: response.data       };     })     .catch(function(error) {       return { success: false };     }); }  getAllData(URLs).then(resp=>{console.log(resp)}).catch(e=>{console.log(e)})
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
like image 20
Mohammed Ashfaq Avatar answered Sep 22 '22 23:09

Mohammed Ashfaq