question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there any function equivalent to always (jQquery)?

See original GitHub issue

I often make an ajax calls with some callbacks with error or not (jQuery ajax always() callback), specially if it’s some recursive functionality, ex:

$.ajax({
    url: "example.php",
    type: 'get',
  })
  .done(function(resp) {
    alert( "success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "complete" ); // <-- always happens
  }); 

I was woundering if there is any axios equivalent? Instead of doing this mess:

axios.get( "example.php").then(response => {
   alert('success');
   alert('complete'); // <--- same thing here
 }).catch(function (error) {
   alert(error);
   alert('complete'); // <-- and here
 });

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

81reactions
lbauschcommented, Mar 29, 2017

Try the following (note the second then):

axios.post('/api/foo', {
    foo: 'bar'
}).then((response) => {
    // success
}).catch((error) => {
    // error
}).then(() => {
    // always executed
})
15reactions
acidjazzcommented, Oct 9, 2018

I’d like to point out a couple things:

  1. The second .then() won’t fire if the .catch() is not specified.
  2. Ultimately I think a .always() would be a better solution, for syntactic sugar reasons.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there an equivalent to Python's all function in JavaScript or ...
Apparently, it does exist: Array.prototype.every . Example from mdn: function isBigEnough(element, index, array) { return (element >= 10); ...
Read more >
jQuery.get( url [, data ] [, success ] [, dataType ] )Returns: jqXHR
A callback function that is executed if the request succeeds. Required if dataType is provided, but you can use null or jQuery.noop as...
Read more >
5 jQuery.each() Function Examples - SitePoint
jQuery's each() function is used to loop through each element of the target jQuery object — an object that contains one or more...
Read more >
Remove jQuery dependency from the once feature | Drupal.org
once.find (new function not previously possible with the jQuery implementation). Backwards compatibility. If code is relying on an existing once ...
Read more >
You Might Not Need jQuery
jQuery and its cousins are great, and by all means use them if it makes it easier to develop your application. If you're...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found