Timeline for How do I return the response from an asynchronous call?
Current License: CC BY-SA 4.0
89 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jan 26, 2023 at 19:29 | comment | added | sootsnoot | only used to make changes to the DOM, it doesn't need to set a value to be returned from the function making the ajax request. | |
Jan 26, 2023 at 19:26 | comment | added | sootsnoot |
I asked a specific question about the jQuery validator plugin, which got an excellent answer. Basically, the interface for custom validation methods for that plugin requires that the method it calls return a boolean, and therefore the method cannot use ajax unless it specifies async: false . In order to avoid making a synchronous call, you must use the plugin's builtin remote method. That method can internally make an async call, because the result is (cont'd)
|
|
Jan 20, 2023 at 23:17 | comment | added | sootsnoot | The only solution I could come up with was to use the async: false option to $.ajax. And I have been roundly scolded for doing that because it is an anti-pattern, and referred to this post. But I cannot find a different solution in this post! | |
Jan 20, 2023 at 23:14 | comment | added | sootsnoot | This answer has lots of great information, but I don't think it has a solution to my problem. I'm using the jQuery validator plugin. The plugin has you call its addMethod() function to define a function that it will call on an input element whose name attribute you specify. The function you define must return true or false. But I need to use $.ajax to check with the server if a value is valid. So I need the method called by the validator to return the result of the $.ajax call. I don't have the option of passing in a function to my validator to be called on ajax completion. (cont'd) | |
Apr 26, 2022 at 21:47 | comment | added | César Rodriguez | The only thing I can see is: JavaScript is an enemy to clean code/architecture. As soon as you need to call anything returning a promise, bid farewell to any separation of concerns in your code: any such module or code will need to end up in a callback hell which ends up outputting data (whatever would be the now-toilet-flushed objective of the module or class, it will now obey the Single-Responsibility-Plus-Output Principle). | |
Apr 8, 2022 at 13:02 | history | edited | Nick Parsons | CC BY-SA 4.0 |
Updated note about being able to use `await` at the top-level of a module
|
Mar 21, 2022 at 9:15 | history | edited | Christian Vincenzo Traina | CC BY-SA 4.0 |
deleted 6 characters in body
|
Mar 21, 2022 at 9:15 | comment | added | Christian Vincenzo Traina |
return await is a mistake and should always be avoided
|
|
Dec 31, 2021 at 9:43 | comment | added | Felix Kling |
@Kadio: Not necessarily. An async function returns a promise, so you can also pass a callback to the promise's then method: asyncFunction().then(result => ...) .
|
|
Dec 30, 2021 at 18:18 | comment | added | Kadio | So I must wrap the main function in an async function and then use await in order to get a value from an async function, right? | |
S May 7, 2021 at 10:09 | history | suggested | Henke - Нава́льный П с м | CC BY-SA 4.0 |
About adding stack snippets to a post - "If the snippet demonstrates the issue the OP is asking about when you click Run code snippet, approve. If not, reject". See https://meta.stackoverflow.com/a/348677.
|
May 7, 2021 at 7:08 | review | Suggested edits | |||
S May 7, 2021 at 10:09 | |||||
Apr 9, 2021 at 11:40 | history | edited | Peter Mortensen | CC BY-SA 4.0 |
Active reading.
|
Oct 6, 2020 at 10:05 | history | edited | Harry B | CC BY-SA 4.0 |
Simply is subjective. Code is hard.
|
Aug 10, 2020 at 15:15 | review | Suggested edits | |||
Aug 11, 2020 at 7:56 | |||||
Aug 4, 2020 at 20:08 | review | Suggested edits | |||
Aug 5, 2020 at 4:13 | |||||
Jul 14, 2020 at 3:23 | history | edited | Maxim Mazurok | CC BY-SA 4.0 |
fixed grammar, updated links
|
S Nov 24, 2019 at 11:19 | history | suggested | Reza Ramezanpour | CC BY-SA 4.0 |
spell fixes
|
Nov 24, 2019 at 6:47 | review | Suggested edits | |||
S Nov 24, 2019 at 11:19 | |||||
Mar 3, 2019 at 17:28 | history | edited | Skippy le Grand Gourou | CC BY-SA 4.0 |
definition and link for IIFE
|
Dec 11, 2018 at 16:51 | history | edited | 3limin4t0r | CC BY-SA 4.0 |
(function) expressions should end with a semicolon
|
Dec 11, 2018 at 12:30 | history | edited | mikemaccana | CC BY-SA 4.0 |
You don't need to use .then() at the top level, make an IIFE.
|
Oct 11, 2018 at 11:07 | history | edited | mikemaccana | CC BY-SA 4.0 |
Mention 'await' is now current (not 'latest')
|
S Sep 28, 2018 at 2:17 | history | suggested | Cavell Blood | CC BY-SA 4.0 |
changed arrows to html entities
|
Sep 28, 2018 at 1:26 | review | Suggested edits | |||
S Sep 28, 2018 at 2:17 | |||||
Aug 27, 2018 at 11:15 | history | edited | wake-0 | CC BY-SA 4.0 |
Removed unused 1000ms and updated 1s comment, so that it is easier to follow.
|
Jun 13, 2018 at 12:51 | history | edited | David Pine | CC BY-SA 4.0 |
deleted 19 characters in body
|
May 15, 2018 at 10:37 | history | bounty ended | user70960 | ||
Mar 1, 2018 at 1:03 | history | edited | whackamadoodle3000 | CC BY-SA 3.0 |
added 1 character in body
|
Dec 25, 2017 at 17:01 | review | Suggested edits | |||
Dec 25, 2017 at 19:22 | |||||
Dec 25, 2017 at 15:52 | review | Suggested edits | |||
Dec 25, 2017 at 16:48 | |||||
Dec 25, 2017 at 14:19 | review | Suggested edits | |||
Dec 25, 2017 at 15:42 | |||||
S Dec 25, 2017 at 8:29 | history | suggested | Farzad Karimi | CC BY-SA 3.0 |
improved syntax
|
Dec 25, 2017 at 4:42 | review | Suggested edits | |||
S Dec 25, 2017 at 8:29 | |||||
S Oct 19, 2017 at 13:35 | history | suggested | bravehart_sf | CC BY-SA 3.0 |
added reference
|
Oct 19, 2017 at 12:18 | review | Suggested edits | |||
S Oct 19, 2017 at 13:35 | |||||
S Oct 18, 2017 at 8:33 | history | suggested | Farzad Karimi | CC BY-SA 3.0 |
improved syntax
|
Oct 18, 2017 at 7:33 | review | Suggested edits | |||
S Oct 18, 2017 at 8:33 | |||||
Jul 11, 2017 at 0:12 | review | Suggested edits | |||
Jul 11, 2017 at 0:30 | |||||
S Jun 25, 2017 at 20:23 | history | suggested | meridius | CC BY-SA 3.0 |
fixed typos
|
Jun 25, 2017 at 20:22 | review | Suggested edits | |||
S Jun 25, 2017 at 20:23 | |||||
Jun 12, 2017 at 11:31 | history | edited | mikemaccana | CC BY-SA 3.0 |
added 76 characters in body
|
Jun 12, 2017 at 11:15 | history | edited | mikemaccana | CC BY-SA 3.0 |
deleted 5 characters in body
|
Jun 12, 2017 at 11:01 | history | edited | mikemaccana | CC BY-SA 3.0 |
Move 'await' to top
|
Jun 2, 2017 at 16:48 | history | edited | Felix Kling | CC BY-SA 3.0 |
added information about async/await; some cleanup
|
May 23, 2017 at 11:55 | history | edited | URL Rewriter Bot |
replaced http://stackoverflow.com/ with https://stackoverflow.com/
|
|
Mar 6, 2017 at 10:19 | review | Suggested edits | |||
Mar 6, 2017 at 12:03 | |||||
Feb 23, 2017 at 11:38 | review | Suggested edits | |||
Feb 23, 2017 at 12:54 | |||||
Dec 17, 2016 at 15:14 | history | rollback | Makyen♦ |
Rollback to Revision 23
|
|
Dec 17, 2016 at 2:38 | comment | added | Makyen♦ |
@MadaraUchiha:(cont) This is now seen by newbies as too specific and not applicable to their question (which could be for any async function, not just $.ajax() ). $.ajax() is just used as an example of the more general issue. Note that I am only here at this question/answer now because what I describe as the response newbies is what has happened when I suggested this as a dup target on a question. If you were not a moderator, I would just revert the edit while we discuss it, but I don't want to, potentially, get into a rollback contest with a moderator.
|
|
Dec 17, 2016 at 2:38 | comment | added | Makyen♦ |
@MadaraUchiha, I strongly disagree with your recent edit (the large TL;DR: Use Promises). This question is a dup target for a large number of general async-issue questions (how to return a variable). Putting that code/info at the start is confusing to the people who we are telling this is a duplicate of their problem. The code you provided only shows how to use the Promise that is auto-returned by jQuery's $.ajax() when you don't provide a callback. You have changed a general purpose answer into one which is now only specific to $.ajax() , or functions which auto-return a Promise.(cont)
|
|
Dec 16, 2016 at 9:18 | history | edited | Madara's Ghost | CC BY-SA 3.0 |
Add TLDR.
|
Sep 28, 2016 at 4:09 | review | Suggested edits | |||
Sep 28, 2016 at 5:09 | |||||
S Aug 25, 2016 at 14:04 | history | suggested | mhelvens | CC BY-SA 3.0 |
fixed "their" + neutral change to overcome 6 character limit
|
Aug 25, 2016 at 13:22 | review | Suggested edits | |||
S Aug 25, 2016 at 14:04 | |||||
Mar 8, 2016 at 22:16 | history | edited | Alvin Thompson | CC BY-SA 3.0 |
added 1 character in body
|
S Jan 16, 2016 at 13:00 | history | suggested | Jed Fox | CC BY-SA 3.0 |
Added caniuse link for Promises.
|
Jan 16, 2016 at 11:48 | review | Suggested edits | |||
S Jan 16, 2016 at 13:00 | |||||
Nov 21, 2015 at 12:17 | history | edited | Peter Mortensen | CC BY-SA 3.0 |
Copy edited (e.g. ref. <http://en.wikipedia.org/wiki/Ajax_%28programming%29>).
|
Oct 14, 2015 at 14:33 | history | edited | ThiefMaster | CC BY-SA 3.0 |
fix typo
|
Oct 8, 2015 at 17:44 | comment | added | Felix Kling |
@Jessi: I think you misunderstood that part of the answer. You cannot use $.getJSON if you want the Ajax request to be synchronous. However, you should not event want the request to be synchronous, so that doesn't apply. You should be using callbacks or promises to handle the response, as it is explained earlier in the answer.
|
|
Jul 31, 2015 at 2:30 | history | edited | Felix Kling | CC BY-SA 3.0 |
added 14 characters in body
|
Jun 24, 2015 at 8:29 | history | edited | Bergi | CC BY-SA 3.0 |
avoid the [promise constructor antipattern](http://stackoverflow.com/q/23803743/1048572) (with native xhr, not using `Promise.resolve` on `$.ajax(…)`). Though maybe we should move that complete section to Benjamins "no jQuery" answer.
|
May 27, 2015 at 14:41 | history | edited | Trott | CC BY-SA 3.0 |
small but important typo fix
|
May 19, 2015 at 17:28 | history | edited | Felix Kling | CC BY-SA 3.0 |
Gave promises their own section; moved "synchronous Ajax" to the bottom
|
Jan 26, 2015 at 8:48 | review | Suggested edits | |||
Jan 26, 2015 at 9:13 | |||||
Oct 10, 2014 at 20:25 | history | edited | Felix Kling | CC BY-SA 3.0 |
added 151 characters in body
|
Jan 8, 2014 at 20:40 | history | bounty ended | apsillers | ||
S Jan 8, 2014 at 14:52 | history | suggested | scniro | CC BY-SA 3.0 |
adding some block code formatting within explanations
|
Jan 8, 2014 at 14:50 | review | Suggested edits | |||
S Jan 8, 2014 at 14:52 | |||||
Dec 13, 2013 at 23:56 | history | edited | megawac | CC BY-SA 3.0 |
minor fix to `xhr.open` - async is third param
|
S Oct 24, 2013 at 15:28 | history | suggested | Venkata Krishna | CC BY-SA 3.0 |
Spelling correction - responseText
|
Oct 24, 2013 at 15:25 | review | Suggested edits | |||
S Oct 24, 2013 at 15:28 | |||||
Apr 16, 2013 at 2:45 | comment | added | Chris Moschini | The Chat for this question is dead so I'm not sure where to propose outlined changes, but I propose: 1) Change the synchronous part to a simple discussion of why it's bad with no code example of how to do it. 2) Remove/merge the callback examples to only show the more flexible Deferred approach, which I think may also be a little easier to follow for those learning Javascript. | |
Apr 16, 2013 at 2:19 | history | edited | Chris Moschini | CC BY-SA 3.0 |
Added an example of turning synchronous bad assumptions into asynchronous valid code
|
Feb 6, 2013 at 23:34 | history | edited | Felix Kling | CC BY-SA 3.0 |
added 531 characters in body
|
Feb 6, 2013 at 23:29 | comment | added | Felix Kling |
@gibberish: Mmmh, I don't know how it can be made clearer. Do you see how foo is called and a function is passed to it (foo(function(result) {....}); )? result is used inside this function and is the response of the Ajax request. To refer to this function, the first parameter of foo is called callback and assigned to success instead of an anonymous function. So, $.ajax will call callback when the request was successful. I tried to explain it a bit more.
|
|
Feb 6, 2013 at 21:56 | history | edited | Felix Kling | CC BY-SA 3.0 |
added 71 characters in body
|
Feb 6, 2013 at 21:07 | comment | added | cssyphus |
In Solution 1, sub jQuery, I could not understand this line: If you use any other jQuery AJAX method, such as $.get, $.getJSON, etc., you have them to $.ajax. (Yes, I realize my nick is a tad ironic in this case)
|
|
Jan 21, 2013 at 14:33 | history | edited | Felix Kling | CC BY-SA 3.0 |
some minor restructuring
|
Jan 19, 2013 at 13:46 | history | edited | Felix Kling | CC BY-SA 3.0 |
added 420 characters in body
|
Jan 19, 2013 at 13:25 | history | edited | Felix Kling | CC BY-SA 3.0 |
added 14 characters in body
|
Jan 17, 2013 at 15:10 | history | edited | Felix Kling | CC BY-SA 3.0 |
improved formatting
|
Jan 17, 2013 at 11:10 | history | edited | Felix Kling | CC BY-SA 3.0 |
deleted 31 characters in body
|
Jan 17, 2013 at 11:07 | vote | accept | Felix Kling | ||
Jan 17, 2013 at 10:47 | comment | added | Felix Kling | @Pommy: If you want to use jQuery, you have to include it. Please refer to docs.jquery.com/Tutorials:Getting_Started_with_jQuery. | |
Jan 9, 2013 at 12:07 | history | edited | Felix Kling | CC BY-SA 3.0 |
some more details
|
Jan 8, 2013 at 17:14 | history | wiki removed | casperOne | ||
Jan 8, 2013 at 17:06 | history | answered | Felix Kling | CC BY-SA 3.0 |