javascript - Promise deferred ajax api jQuery -


I do not understand the promises / too much postponement ... I have something like this:

< Pre> function callAjax () {return $. Ajax ({type: 'post', data type: 'jsonp', data: {condition: position, name: name}, url: '/ test', xhrFields: {with credentials: true}}); } Function connectAjax () {var msg = 'does not work; Var promise = callAge (); promise. Then (function (data, textstats, exhaust) {msg = 'it worked!';}, Function (data, textstats, exhaor) {msg = 'it failed!';}); Console.log (msg); // Output 'does not work'}

I have tried many different things (always, done, etc ..) but it could not work.

I use jsonp but my request is not cross domain. I expect 500 errors from server for my request.

For your example of working, you <...

Keep in mind that the promise callback function is called, only when the AJAX call ends your script does not wait until it happens and 'console.log (msg);' The AJAX call is executed before the return.

This is a great example of the non-blocking nature of Javascript.

For more detailed understanding of JS Event Loop:


Comments