vue resource promise callback

11,787

Simply do the call again and make sure you return the Promise created by it:

methods: { someMethod: function(){
  var resource = this.$resource('index');
  resource.save({name: 'jimmy'})
    .then(function (response) {

      //resend request lets say if response.data == 'test'
      if (response.data === 'test') {
        // do request again and return the Promise.
        return resource.save({name: 'jimmy'})
      } else {
        return Promise.resolve(response)
      }
    })
    .then(function(response) {
      // do something with response
      // if there was a retry, `response` will be the second one.
    })
    .catch(function (error) {

    // catch() will catch any errors in the Promise chain, not just the first level.
      console.log(error)
    });
  }
}
Share:
11,787

Related videos on Youtube

Jimmy Obonyo Abor
Author by

Jimmy Obonyo Abor

Full stack web/mobile developer, cloud solutions architect. I am vastly experienced in server/web/mobile/cloud/containerization technologies, conversant with functional and object oriented methodologies , test driven programming, ci/cd technologies and processes. Worked on singular and team settings, able to thrive in a fast-paced environment, looking to create world-class applications. Am passionate about programming, loves to learn, enjoys the challenge and artistry inherent in the creation process. Can do attitude, reliable, self starter. My favorite stacks are Nodejs, Python, Go-lang,Meteor, React,React Native,Vue, Laravel (Php) , Jest, Mocha.

Updated on June 04, 2022

Comments

  • Jimmy Obonyo Abor
    Jimmy Obonyo Abor over 1 year

    Id like to parse a vue resource data and send callback request depending on the data i receive from server , how would i achive this either using Vue.interceptors or .then callback :

    methods : function(){
    var resource = this.$resource('index');
    resource.save({name: 'jimmy'}).then(function (response) {
        //success callback
        //resend request lets say if response.data == 'test'
    
    }, function (response) {
       // error callback
       console.log(response)
    });
    }
    
  • Jimmy Obonyo Abor
    Jimmy Obonyo Abor over 7 years
    Thanks for the answer , i was however thinking of a chain promise , or an interceptor version of achieving this .
  • Linus Borg
    Linus Borg over 7 years
    I edited my comment to improve the chaining, but i can't think of a way to do this with Interceptors. They are not meant for retrys, but for manipulating the request/result globally (to insert headers etc)