async function firstAsync() {
let promise = new Promise((res, rej) => {
setTimeout(() => res("Now it's done!"), 1000)
});
// wait until the promise returns us a value
let result = await promise;
// "Now it's done!"
alert(result);
}
};
firstAsync();
please tell me how can i wrap jquery ajax call in async and await function ?
i know jquery ajax is by default async kind function.
if i wrap jquery ajax call in async and await function with promise usage then will get any advantage ?
// Assign handlers immediately after making the request,
// and remember the jqXHR object for this request
var jqxhr = $.ajax( "example.php" )
.done(function() {
alert( "success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "complete" );
});
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always(function() {
alert( "second complete" );
});
as $.ajax already return a promise you can just use await (if your browser supports await/async or you transpile the javascript)
// using await
async function myasync1(url) {
const response = await $.ajax(url);
return response;
}
// as there is no real processing after the await
// can just return the original promise
function myasync2(url) {
return $.ajax(url);
}
// use immediate async function to call with await
const url = "...";
(async function() {
const r1 = await myasync1(url);
const r2 = await myasync2(url);
})();
what is difference between done, success and always callback ?
According to the openly published reference linked to above, done() and always() are promises. Success is a callback. The done() promise replaces the success callback. See you other threads where always() is covers.
jquery predates javascripts definition of promises. jQuery had deferred, which was a promise like utility probably influenced by dojo’s deferred. When promises become popular, jQueries differed was modified to also be promise compliant. JavaScript promises
are a defined api, so multiple promise libraries can interact.
For compatible with older code, jQueries Ajax returns a deferred, which is now a superset of a promise. Also the deferred resolves support more than 1 parameter.
Member
77 Points
150 Posts
How can i wrap jquery ajax call in async and await function with promise usage
Oct 07, 2020 04:48 PM|TDP|LINK
please tell me how can i wrap jquery ajax call in async and await function ?
i know jquery ajax is by default async kind function.
if i wrap jquery ajax call in async and await function with promise usage then will get any advantage ?
All-Star
53631 Points
23985 Posts
Re: How can i wrap jquery ajax call in async and await function with promise usage
Oct 07, 2020 05:45 PM|mgebhard|LINK
jQuery AJAX returns promises. Please see the docs.
https://api.jquery.com/jquery.ajax/
All-Star
58444 Points
15764 Posts
Re: How can i wrap jquery ajax call in async and await function with promise usage
Oct 08, 2020 05:15 PM|bruce (sqlwork.com)|LINK
as $.ajax already return a promise you can just use await (if your browser supports await/async or you transpile the javascript)
Member
77 Points
150 Posts
Re: How can i wrap jquery ajax call in async and await function with promise usage
Oct 09, 2020 12:47 PM|TDP|LINK
i never use always callback what it does ?
i use done and success for ajax call. what always does ? when it fire ? how it is different from done & success ?
All-Star
53631 Points
23985 Posts
Re: How can i wrap jquery ajax call in async and await function with promise usage
Oct 09, 2020 01:08 PM|mgebhard|LINK
Well, the "always" promise executes when the asynchronous operation completes. This information is openly explained in the linked documentation above.
jqXHR.always(function( data|jqXHR, textStatus, jqXHR|errorThrown ) { }); (added in jQuery 1.6)
An alternative construct to the complete callback option, the
.always()
method replaces the deprecated.complete()
method.All-Star
58444 Points
15764 Posts
Re: How can i wrap jquery ajax call in async and await function with promise usage
Oct 10, 2020 03:33 PM|bruce (sqlwork.com)|LINK
in await syntax, always is
Member
77 Points
150 Posts
Re: How can i wrap jquery ajax call in async and await function with promise usage
Oct 11, 2020 08:31 AM|TDP|LINK
what is difference between done, success and always callback ?
i use success in jquery ajax. when response comes from server side then success callback fire.
some people use done instead of success callback. so curious to know the difference between success and always callback?
when to use success and when to use done and when to use always . please share the knowledge. thanks
All-Star
53631 Points
23985 Posts
Re: How can i wrap jquery ajax call in async and await function with promise usage
Oct 11, 2020 11:40 AM|mgebhard|LINK
According to the openly published reference linked to above, done() and always() are promises. Success is a callback. The done() promise replaces the success callback. See you other threads where always() is covers.
Reference
https://api.jquery.com/jquery.ajax/
All-Star
58444 Points
15764 Posts
Re: How can i wrap jquery ajax call in async and await function with promise usage
Oct 11, 2020 03:47 PM|bruce (sqlwork.com)|LINK
jquery predates javascripts definition of promises. jQuery had deferred, which was a promise like utility probably influenced by dojo’s deferred. When promises become popular, jQueries differed was modified to also be promise compliant. JavaScript promises are a defined api, so multiple promise libraries can interact.
For compatible with older code, jQueries Ajax returns a deferred, which is now a superset of a promise. Also the deferred resolves support more than 1 parameter.