Skip to main content
Active reading [<https://en.wikipedia.org/wiki/JavaScript>]. Reduced the excessive (unnecessary) formatting. "stuff" is an uncountable noun in this context.
Source Link
Peter Mortensen
  • 31.6k
  • 22
  • 109
  • 132

This is one of the places which two ways-way data binding or store concept that's used in many new JavaScript frameworks will work great for you...

So if you are using Angular, ReactAngular, React, or any other frameworks which do two ways data bindingtwo-way data binding or store conceptstore concept, this issue is simply fixed for you, so in easy wordwords, your result is undefined at the first stage, so you have got result = undefined before you receive the data, then as soon as you get the result, it will be updated and get assigned to the new value which response of your Ajax call...

But how you can do it in pure javascriptJavaScript or jQueryjQuery for example as you asked in this question?

You can use a callbackcallback, promisepromise and recently observableobservable to handle it for you, for. For example, in promises we have some function like success() or then() which will be executed when your data is ready for you,. The same with callback or the subscribesubscribe function on observablean observable.

For example, in your case which you are using jQueryjQuery, you can do something like this:

$(document).ready(function(){
    function foo() {
        $.ajax({url: "api/data", success: function(data){
            fooDone(data); //after After we have data, we pass it to fooDone
        }});
    };

    function fooDone(data) {
        console.log(data); // fooDone has the data and console.log it
    };
    
    foo(); // The call happens here
});

For more information, study about promisespromises and observablesobservables which are newer ways to do this async stuffsstuff.

This is one of the places which two ways data binding or store concept that's used in many new JavaScript frameworks will work great for you...

So if you are using Angular, React or any other frameworks which do two ways data binding or store concept this issue is simply fixed for you, so in easy word, your result is undefined at the first stage, so you have got result = undefined before you receive the data, then as soon as you get the result, it will be updated and get assigned to the new value which response of your Ajax call...

But how you can do it in pure javascript or jQuery for example as you asked in this question?

You can use a callback, promise and recently observable to handle it for you, for example in promises we have some function like success() or then() which will be executed when your data is ready for you, same with callback or subscribe function on observable.

For example in your case which you are using jQuery, you can do something like this:

$(document).ready(function(){
    function foo() {
        $.ajax({url: "api/data", success: function(data){
            fooDone(data); //after we have data, we pass it to fooDone
        }});
    };

    function fooDone(data) {
        console.log(data); //fooDone has the data and console.log it
    };
    
    foo(); //call happens here
});

For more information study about promises and observables which are newer ways to do this async stuffs.

This is one of the places which two-way data binding or store concept that's used in many new JavaScript frameworks will work great for you...

So if you are using Angular, React, or any other frameworks which do two-way data binding or store concept, this issue is simply fixed for you, so in easy words, your result is undefined at the first stage, so you have got result = undefined before you receive the data, then as soon as you get the result, it will be updated and get assigned to the new value which response of your Ajax call...

But how you can do it in pure JavaScript or jQuery for example as you asked in this question?

You can use a callback, promise and recently observable to handle it for you. For example, in promises we have some function like success() or then() which will be executed when your data is ready for you. The same with callback or the subscribe function on an observable.

For example, in your case which you are using jQuery, you can do something like this:

$(document).ready(function(){
    function foo() {
        $.ajax({url: "api/data", success: function(data){
            fooDone(data); // After we have data, we pass it to fooDone
        }});
    };

    function fooDone(data) {
        console.log(data); // fooDone has the data and console.log it
    };

    foo(); // The call happens here
});

For more information, study promises and observables which are newer ways to do this async stuff.

added 21 characters in body
Source Link
Alireza
  • 104k
  • 27
  • 276
  • 173

This is one of the places which two ways data binding or store concept that's used in many new JavaScript frameworks will work great for you...

So if you are using Angular, React or any other frameworks which do two ways data binding or store concept this issue is simply fixed for you, so in easy word, your result is undefined at the first stage, so you have got result = undefined before you receive the data, then as soon as you get the result, it will be updated and get assigned to the new value which response of your Ajax call...

But how you can do it in pure javascript or jQuery for example as you asked in this question?

You can use a callback, promise and recently observable to handle it for you, for example in promises we have some function like success() or then() which will be executed when your data is ready for you, same with callback or subscribe function on observable.

For example in your case which you are using jQuery, you can do something like this:

$(document).ready(function(){
    function foo() {
        $.ajax({url: "api/data", success: function(data){
            fooDone(data); //after we have data, we pass it to fooDone
        }});
    };

    function fooDone(data) {
        console.log(data); //fooDone has the data and console.log it
    };
    
    foo(); //call happens here
});

For more information study about promises and observables which are newer ways to do this async stuffs.

This is one of the places which two ways data binding that's used in many new JavaScript frameworks will work great for you...

So if you are using Angular, React or any other frameworks which do two ways data binding or store concept this issue is simply fixed for you, so in easy word, your result is undefined at the first stage, so you have got result = undefined before you receive the data, then as soon as you get the result, it will be updated and get assigned to the new value which response of your Ajax call...

But how you can do it in pure javascript or jQuery for example as you asked in this question?

You can use a callback, promise and recently observable to handle it for you, for example in promises we have some function like success() or then() which will be executed when your data is ready for you, same with callback or subscribe function on observable.

For example in your case which you are using jQuery, you can do something like this:

$(document).ready(function(){
    function foo() {
        $.ajax({url: "api/data", success: function(data){
            fooDone(data); //after we have data, we pass it to fooDone
        }});
    };

    function fooDone(data) {
        console.log(data); //fooDone has the data and console.log it
    };
    
    foo(); //call happens here
});

For more information study about promises and observables which are newer ways to do this async stuffs.

This is one of the places which two ways data binding or store concept that's used in many new JavaScript frameworks will work great for you...

So if you are using Angular, React or any other frameworks which do two ways data binding or store concept this issue is simply fixed for you, so in easy word, your result is undefined at the first stage, so you have got result = undefined before you receive the data, then as soon as you get the result, it will be updated and get assigned to the new value which response of your Ajax call...

But how you can do it in pure javascript or jQuery for example as you asked in this question?

You can use a callback, promise and recently observable to handle it for you, for example in promises we have some function like success() or then() which will be executed when your data is ready for you, same with callback or subscribe function on observable.

For example in your case which you are using jQuery, you can do something like this:

$(document).ready(function(){
    function foo() {
        $.ajax({url: "api/data", success: function(data){
            fooDone(data); //after we have data, we pass it to fooDone
        }});
    };

    function fooDone(data) {
        console.log(data); //fooDone has the data and console.log it
    };
    
    foo(); //call happens here
});

For more information study about promises and observables which are newer ways to do this async stuffs.

added 4 characters in body
Source Link
Alireza
  • 104k
  • 27
  • 276
  • 173

This is one of the places which two ways data binding that's used in many new JavaScript frameworks will work great for you...

So if you are using Angular, React or any other frameworks which do two ways data binding or store concept this issue is simply fixed for you, so in easy word, your result is undefined at the first stage, so you have got result = undefined before you receive the data, then as soon as you get the result, it will be updated and get assigned to the new value which response of your Ajax call...

But how you can do it in pure javascript or jQuery for example as you asked in this question?

You can use a callback, promise and recently observable to handle it for you, for example in promises we have some function like success()success() or then()then() which will be executed when your data is ready for you, same with callback or subscribe function on observable.

For example in your case which you are using jQuery, you can do something like this:

$(document).ready(function(){
    function foo() {
        $.ajax({url: "api/data", success: function(data){
            fooDone(data); //after we have data, we pass it to fooDone
        }});
    };

    function fooDone(data) {
        console.log(data); //fooDone has the data and console.log it
    };
    
    foo(); //call happens here
});

For more information study about promises and observables which are newer ways to do this async stuffs.

This is one of the places which two ways data binding that's used in many new JavaScript frameworks will work great for you...

So if you are using Angular, React or any other frameworks which do two ways data binding or store concept this issue is simply fixed for you, so in easy word, your result is undefined at the first stage, so you have got result = undefined before you receive the data, then as soon as you get the result, it will be updated and get assigned to the new value which response of your Ajax call...

But how you can do it in pure javascript or jQuery for example as you asked in this question?

You can use a callback, promise and recently observable to handle it for you, for example in promises we have some function like success() or then() which will be executed when your data is ready for you, same with callback or subscribe function on observable.

For example in your case which you are using jQuery, you can do something like this:

$(document).ready(function(){
    function foo() {
        $.ajax({url: "api/data", success: function(data){
            fooDone(data); //after we have data, we pass it to fooDone
        }});
    };

    function fooDone(data) {
        console.log(data); //fooDone has the data and console.log it
    };
    
    foo(); //call happens here
});

For more information study about promises and observables which are newer ways to do this async stuffs.

This is one of the places which two ways data binding that's used in many new JavaScript frameworks will work great for you...

So if you are using Angular, React or any other frameworks which do two ways data binding or store concept this issue is simply fixed for you, so in easy word, your result is undefined at the first stage, so you have got result = undefined before you receive the data, then as soon as you get the result, it will be updated and get assigned to the new value which response of your Ajax call...

But how you can do it in pure javascript or jQuery for example as you asked in this question?

You can use a callback, promise and recently observable to handle it for you, for example in promises we have some function like success() or then() which will be executed when your data is ready for you, same with callback or subscribe function on observable.

For example in your case which you are using jQuery, you can do something like this:

$(document).ready(function(){
    function foo() {
        $.ajax({url: "api/data", success: function(data){
            fooDone(data); //after we have data, we pass it to fooDone
        }});
    };

    function fooDone(data) {
        console.log(data); //fooDone has the data and console.log it
    };
    
    foo(); //call happens here
});

For more information study about promises and observables which are newer ways to do this async stuffs.

added 19 characters in body
Source Link
Alireza
  • 104k
  • 27
  • 276
  • 173
Loading
added 4 characters in body
Source Link
Alireza
  • 104k
  • 27
  • 276
  • 173
Loading
added 10 characters in body
Source Link
Alireza
  • 104k
  • 27
  • 276
  • 173
Loading
deleted 1 character in body
Source Link
Alireza
  • 104k
  • 27
  • 276
  • 173
Loading
added 3 characters in body
Source Link
Alireza
  • 104k
  • 27
  • 276
  • 173
Loading
added 8 characters in body
Source Link
Alireza
  • 104k
  • 27
  • 276
  • 173
Loading
added 3 characters in body
Source Link
Alireza
  • 104k
  • 27
  • 276
  • 173
Loading
added 11 characters in body
Source Link
Alireza
  • 104k
  • 27
  • 276
  • 173
Loading
deleted 3 characters in body
Source Link
Alireza
  • 104k
  • 27
  • 276
  • 173
Loading
added 13 characters in body
Source Link
Alireza
  • 104k
  • 27
  • 276
  • 173
Loading
added 1 character in body
Source Link
Alireza
  • 104k
  • 27
  • 276
  • 173
Loading
added 14 characters in body
Source Link
Alireza
  • 104k
  • 27
  • 276
  • 173
Loading
Source Link
Alireza
  • 104k
  • 27
  • 276
  • 173
Loading