JavaScript: How to return an outer function inside an asynchronous inner function?

Melkor

I know I can use an external variable to identify some status the outer function needs to do with. But consider this: If the inner function is asynchronous? The outer function will not wait for the variable of inner function will change, so how can I return the outer function now?

function outer() {
    var flag = true;
    // For example, a jquery-like ajax call
    $.ajax({
        // Some settings
        error: function(jqXHR, textStatus, errorThrown) {
            // Here I want to return outer()
            flag = false;
        }
    });
    return flag;
}

So as you can see, if I use flag as the return value, outer() will very likely return true because the ajax call may take a long time. And for the same reason, I don't want to set async: false because that will stop the page reaction.

keune

Your outer function will return immediately, so you will always get true as flag's value. In order to get the right value, you need to let the async function do its job and get back to you when it is ready. Consider this:

function outer(cb) {
    var flag = true;
    // For example, a jquery-like ajax call
    $.ajax({
        // Some settings
        error: function (jqXHR, textStatus, errorThrown) {
            // Here I want to return outer()
            flag = false;
            cb(flag);
        },
        success: function () {
            flag = true; // or whatever value you need.
            cb(flag);
        }
    });
}

function callback(flag) {
    // this function will be called after the ajax is complete.
    // real value of flag variable will be available here
    console.log(flag);
}
outer(callback);

You pass a function as a parameter to outer function, and it calls that function when ajax is complete with the value you need as a parameter. This way you will get the real result.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JavaScript: How to return an outer function inside an asynchronous inner function?

From Dev

How to return an outer function inside an asynchronous inner function in swift?

From Dev

How to access function inside function without outer returning inner one

From Dev

Do function arguments of an outer function change for an asynchronous inner function?

From Dev

How to return a value to an outer Javascript function

From Dev

How to return a function value from inside an inner function/stream listener?

From Dev

How to return a function value from inside an inner function/stream listener?

From Dev

How to access outer object property with inner object function Javascript

From Dev

How to assign a value to an outer variable from an inner function in javascript?

From Dev

how to return a function inside a function javascript/nodejs?

From Dev

Is it possible to return from an outer function from within an inner function?

From Dev

How to access outer scope from inner function?

From Dev

How javascript retains the outer function's execution context when an inner function is returned?

From Dev

How to assign global variable inside of an asynchronous function in Javascript?

From Dev

How do you call an outer functions return after an inner async function finishes running?

From Dev

How do you call an outer functions return after an inner async function finishes running?

From Dev

How to return a function inside a function

From Dev

node js - how do I return a value from a function when it depends on asynchronous function inside

From Dev

How to access outer function variable in nested inner function in JS

From Dev

How to resolve an outer async function from an inner function

From Dev

How can I use argument of outer function in an inner function in php?

From Dev

How to access the inner function's variable from outer function with jQuery?

From Dev

How inner anonymous function have scope of outer function?

From Dev

How to stop asynchronous function in JavaScript?

From Dev

Javascript return value from asynchronous function

From Dev

Javascript return inner function value with delay

From Java

How to return value from an asynchronous callback function?

From Dev

Mock a response of a inner function but test the outer function

From Dev

Passing id returned by outer function to inner function

Related Related

  1. 1

    JavaScript: How to return an outer function inside an asynchronous inner function?

  2. 2

    How to return an outer function inside an asynchronous inner function in swift?

  3. 3

    How to access function inside function without outer returning inner one

  4. 4

    Do function arguments of an outer function change for an asynchronous inner function?

  5. 5

    How to return a value to an outer Javascript function

  6. 6

    How to return a function value from inside an inner function/stream listener?

  7. 7

    How to return a function value from inside an inner function/stream listener?

  8. 8

    How to access outer object property with inner object function Javascript

  9. 9

    How to assign a value to an outer variable from an inner function in javascript?

  10. 10

    how to return a function inside a function javascript/nodejs?

  11. 11

    Is it possible to return from an outer function from within an inner function?

  12. 12

    How to access outer scope from inner function?

  13. 13

    How javascript retains the outer function's execution context when an inner function is returned?

  14. 14

    How to assign global variable inside of an asynchronous function in Javascript?

  15. 15

    How do you call an outer functions return after an inner async function finishes running?

  16. 16

    How do you call an outer functions return after an inner async function finishes running?

  17. 17

    How to return a function inside a function

  18. 18

    node js - how do I return a value from a function when it depends on asynchronous function inside

  19. 19

    How to access outer function variable in nested inner function in JS

  20. 20

    How to resolve an outer async function from an inner function

  21. 21

    How can I use argument of outer function in an inner function in php?

  22. 22

    How to access the inner function's variable from outer function with jQuery?

  23. 23

    How inner anonymous function have scope of outer function?

  24. 24

    How to stop asynchronous function in JavaScript?

  25. 25

    Javascript return value from asynchronous function

  26. 26

    Javascript return inner function value with delay

  27. 27

    How to return value from an asynchronous callback function?

  28. 28

    Mock a response of a inner function but test the outer function

  29. 29

    Passing id returned by outer function to inner function

HotTag

Archive