What's C#'s await equivalent in javascript/jquery?
I think Promises is exactly what you are asking.
.promise()
Return a Promise object to observe when all actions of a certain type bound to the collection, queued or not, have finished. e.g.
var div = $( "<div>" );
div.promise().done(function( arg1 ) {
// Will fire right away and alert "true"
alert( this === div && arg1 === div );
});
For more info refer : https://api.jquery.com/promise/
Deferred Promise is closer to async behaviour:
deferred.promise()
The deferred.promise() method allows an asynchronous function to prevent other code from interfering with the progress or status of its internal request. The Promise exposes only the Deferred methods needed to attach additional handlers or determine the state (then
, done
, fail
, always
, pipe
, progress
, and state
), but not ones that change the state (resolve
, reject
, notify
, resolveWith
, rejectWith
, and notifyWith
).
If target is provided, deferred.promise()
will attach the methods onto it and then return this object rather than create a new one. This can be useful to attach the Promise behavior to an object that already exists.
If you are creating a Deferred, keep a reference to the Deferred so that it can be resolved or rejected at some point. Return only the Promise object via deferred.promise()
so other code can register callbacks or inspect the current state.
Example:
function asyncEvent() {
var dfd = new jQuery.Deferred();
// Resolve after a random interval
setTimeout(function() {
dfd.resolve( "hurray" );
}, Math.floor( 400 + Math.random() * 2000 ) );
// Reject after a random interval
setTimeout(function() {
dfd.reject( "sorry" );
}, Math.floor( 400 + Math.random() * 2000 ) );
// Show a "working..." message every half-second
setTimeout(function working() {
if ( dfd.state() === "pending" ) {
dfd.notify( "working... " );
setTimeout( working, 500 );
}
}, 1 );
// Return the Promise so caller can't change the Deferred
return dfd.promise();
}
// Attach a done, fail, and progress handler for the asyncEvent
$.when( asyncEvent() ).then(
function( status ) {
alert( status + ", things are going well" );
},
function( status ) {
alert( status + ", you fail this time" );
},
function( status ) {
$( "body" ).append( status );
}
);
For more information, see the documentation for Deferred object: http://api.jquery.com/category/deferred-object/
jQuery.when()
Provides a way to execute callback functions based on one or more objects, usually Deferred objects that represent asynchronous events.Example:
$.when( $.ajax( "test.aspx" ) ).then(function( data, textStatus, jqXHR ) {
alert( jqXHR.status ); // Alerts 200
});
JavaScript does not (currently) have a language equivalent of async
/await
. There are various promise libraries available for JavaScript that give you a rough equivalent of the Task
type. This is a good step above raw callbacks, but you still end up with awkward nesting or callback spaghetti in all but the simplest scenarios.
JavaScript ECMAScript 6 ("Harmony") is expected to include generators. ES6 is on track to become official later this year, but it will probably be some time after that before you can safely assume that your users' browsers support generators.
By combining generators with promises, you can achieve a true async
/await
equivalent.
You can't. There is no equivalent of await
in JS.
You will have to simulate it by returning false at the invocation of postForm
and then upon execution of the callback-function from the AJAX call close the dialog.
EDIT/UPDATE: Starting with ES2017 there is async/await support - though I don't know if it works in conjunction with jQuery.