Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

Active reading [<https://en.wikipedia.org/wiki/Sentence_clause_structure#Run-on_sentences> <https://en.wiktionary.org/wiki/let%27s#Etymology>]. Used a more direct cross reference (as user names can change at any time).
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 110
  • 134
function foo() {
 var httpRequest = new XMLHttpRequest();
 httpRequest.open('GET', "/echo/json");
 httpRequest.send();
 return httpRequest.responseText;
}
var result = foo(); // alwaysAlways ends up being 'undefined'

Felix Kling did a fine jobFelix Kling did a fine job writing an answer for people using jQuery for AJAX, but I've decided to provide an alternative for people who aren't.

Here is a simple analogy:

function getFive(){
 var a;
 setTimeout(function(){
 a=5;
 },10);
 return a;
}
function onComplete(a){ // When the code completes, do this
 alert(a);
}
function getFive(whenDone){
 var a;
 setTimeout(function(){
 a=5;
 whenDone(a);
 },10);
}
  1. Make the AJAX call synchronous (letslet’s call it SJAX).
  2. Restructure your code to work properly with callbacks.

If you have to do it, you can pass a flag:. Here is how: :

var request = new XMLHttpRequest();
request.open('GET', 'yourURL', false); // `false` makes the request synchronous
request.send(null);

if (request.status === 200) {// That's HTTP for 'ok'
 console.log(request.responseText);
}
var result = foo();
// codeCode that depends on `result` goes here
foo(function(result) {
 // codeCode that depends on `result`
});
function myHandler(result) {
 // codeCode that depends on `result`
}
foo(myHandler);
function foo(callback) {
 var httpRequest = new XMLHttpRequest();
 httpRequest.onload = function(){ // whenWhen the request is loaded
 callback(httpRequest.responseText);// we'reWe're calling our method
 };
 httpRequest.open('GET', "/echo/json");
 httpRequest.send();
}

We have now made our foofoo function accept an action to run when the AJAX completes successfully, we. We can extend this further by checking if the response status is not 200 and acting accordingly (create a fail handler and such). Effectively it is solving our issue.

If you're still having a hard time understanding this, read the AJAX getting started guide at MDN.

function foo() {
 var httpRequest = new XMLHttpRequest();
 httpRequest.open('GET', "/echo/json");
 httpRequest.send();
 return httpRequest.responseText;
}
var result = foo(); // always ends up being 'undefined'

Felix Kling did a fine job writing an answer for people using jQuery for AJAX, I've decided to provide an alternative for people who aren't.

Here is a simple analogy

function getFive(){
 var a;
 setTimeout(function(){
 a=5;
 },10);
 return a;
}
function onComplete(a){ // When the code completes, do this
 alert(a);
}
function getFive(whenDone){
 var a;
 setTimeout(function(){
 a=5;
 whenDone(a);
 },10);
}
  1. Make the AJAX call synchronous (lets call it SJAX).
  2. Restructure your code to work properly with callbacks.

If you have to do it, you can pass a flag: Here is how:

var request = new XMLHttpRequest();
request.open('GET', 'yourURL', false); // `false` makes the request synchronous
request.send(null);

if (request.status === 200) {// That's HTTP for 'ok'
 console.log(request.responseText);
}
var result = foo();
// code that depends on `result` goes here
foo(function(result) {
 // code that depends on `result`
});
function myHandler(result) {
 // code that depends on `result`
}
foo(myHandler);
function foo(callback) {
 var httpRequest = new XMLHttpRequest();
 httpRequest.onload = function(){ // when the request is loaded
 callback(httpRequest.responseText);// we're calling our method
 };
 httpRequest.open('GET', "/echo/json");
 httpRequest.send();
}

We have now made our foo function accept an action to run when the AJAX completes successfully, we can extend this further by checking if the response status is not 200 and acting accordingly (create a fail handler and such). Effectively solving our issue.

If you're still having a hard time understanding this read the AJAX getting started guide at MDN.

function foo() {
 var httpRequest = new XMLHttpRequest();
 httpRequest.open('GET', "/echo/json");
 httpRequest.send();
 return httpRequest.responseText;
}
var result = foo(); // Always ends up being 'undefined'

Felix Kling did a fine job writing an answer for people using jQuery for AJAX, but I've decided to provide an alternative for people who aren't.

Here is a simple analogy:

function getFive(){
 var a;
 setTimeout(function(){
 a=5;
 },10);
 return a;
}
function onComplete(a){ // When the code completes, do this
 alert(a);
}
function getFive(whenDone){
 var a;
 setTimeout(function(){
 a=5;
 whenDone(a);
 },10);
}
  1. Make the AJAX call synchronous (let’s call it SJAX).
  2. Restructure your code to work properly with callbacks.

If you have to do it, you can pass a flag. Here is how :

var request = new XMLHttpRequest();
request.open('GET', 'yourURL', false); // `false` makes the request synchronous
request.send(null);
if (request.status === 200) {// That's HTTP for 'ok'
 console.log(request.responseText);
}
var result = foo();
// Code that depends on `result` goes here
foo(function(result) {
 // Code that depends on `result`
});
function myHandler(result) {
 // Code that depends on `result`
}
foo(myHandler);
function foo(callback) {
 var httpRequest = new XMLHttpRequest();
 httpRequest.onload = function(){ // When the request is loaded
 callback(httpRequest.responseText);// We're calling our method
 };
 httpRequest.open('GET', "/echo/json");
 httpRequest.send();
}

We have now made our foo function accept an action to run when the AJAX completes successfully. We can extend this further by checking if the response status is not 200 and acting accordingly (create a fail handler and such). Effectively it is solving our issue.

If you're still having a hard time understanding this, read the AJAX getting started guide at MDN.

Commonmark migration
Source Link

#If you're not using jQuery in your code, this answer is for you

If you're not using jQuery in your code, this answer is for you

#What you're facing

What you're facing

XMLHttpRequest supports both synchronous and asynchronous communications. In general, however, asynchronous requests should be preferred to synchronous requests for performance reasons.

In short, synchronous requests block the execution of code... ...this can cause serious issues...

#If you're not using jQuery in your code, this answer is for you

#What you're facing

XMLHttpRequest supports both synchronous and asynchronous communications. In general, however, asynchronous requests should be preferred to synchronous requests for performance reasons.

In short, synchronous requests block the execution of code... ...this can cause serious issues...

If you're not using jQuery in your code, this answer is for you

What you're facing

XMLHttpRequest supports both synchronous and asynchronous communications. In general, however, asynchronous requests should be preferred to synchronous requests for performance reasons.

In short, synchronous requests block the execution of code... ...this can cause serious issues...

added 1 character in body
Source Link
Post Undeleted by Benjamin Gruenbaum
Post Deleted by Benjamin Gruenbaum
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot
Loading
added 147 characters in body
Source Link
Benjamin Gruenbaum
  • 277.7k
  • 91
  • 524
  • 524
Loading
Gone are the days of IE7
Source Link
Benjamin Gruenbaum
  • 277.7k
  • 91
  • 524
  • 524
Loading
added 99 characters in body
Source Link
Benjamin Gruenbaum
  • 277.7k
  • 91
  • 524
  • 524
Loading
fixed implied globals and a little off code fomarring
Source Link
Zirak
  • 40k
  • 13
  • 85
  • 92
Loading
Source Link
Benjamin Gruenbaum
  • 277.7k
  • 91
  • 524
  • 524
Loading
lang-js

AltStyle によって変換されたページ (->オリジナル) /