Friday, August 16, 2019

Multiple ajax call

Multiple ajax call

EmailThis Premium lets you save unlimited bookmarks, PDF, DOCX files, PPTs and images. It also gives you a PDF copy of every page that you save. Upgrade to Premium →

Hi all,
I'm going to have multiple ajax call async, but the last one should be calling when the others have finished.
How would I do this?

I have imagine to create something like below with computed property and mdoels, but I'm not sure if it is the best approach.

data() {    return {       ajax1: false,       ajax2: false,       ajax3: false,       ajax4: false,          } } computed: {    ajax5() {       return this.ajax1 && this.ajax2 && this.ajax3 && this.ajax4    }, }  methods: {    getAjax5() {      if (this.ajax5) {         // My cool stuff      }    } } 

What do you think?

Sounds like you have to wrap the calls in a Promise.all().

How would I use this? Appreciate if you could give an example!

Much appreciated!

Two notes about the example, just to be clear:

  1. If your AJAX lib returns a proper Promise (Which $.ajax() doesn't), then its not necessesary to wrap it in an additional promise.
  2. You don't need bluebird, Promise.all() is part of the ES6 Standard API of Promises.
  3. the retun values of those pormises are passed to Promise.all()'s then() callback as an array, and you can easily access them with destructuring:
Promise.all([ajax1, ajax2])   .then(function([result1, result2]) {     console.log('Results': result1, result2);   })   .catch(function(err){     console.log('There is an error', err);   }); 

Hello! Looks like you're enjoying the discussion, but you haven't signed up for an account yet.

When you create an account, we remember exactly what you've read, so you always come right back where you left off. You also get notifications, here and via email, whenever someone replies to you. And you can like posts to share the love. heartpulse

Source: https://forum.vuejs.org/t/multiple-ajax-call/6999

Upgrade to Premium Plan

✔ Save unlimited bookmarks.

✔ Save PDFs, DOCX files, images and Excel sheets as email attachments.

✔ Get priority support and access to latest features.

Upgrade to Premium

No comments: