IF statement control flow and asynchronous function

* FOR SOFTWARE CONSULTING CHECK http://hackerhitch.com/ *

My code is structured as follows:

IF (something) {
..stuff
..Asynchronous Function Call
}
ELSE (something) {
..stuff
..Asynchronous Function Call
}
..more stuff

Assume that the IF condition is met, the code fulfills the "stuff", and then proceeds to call the asynchronous function. Will it just make a call, but exit the IF statement and execute “more stuff” on average, assuming waiting for the asynchronous function call to complete?

OR

Does it complete the completion of an asynchronous function call, and then continue with “more things,” like a regular IF statement block does.

In the previous case, any recommendations on how to provide an asynchronous function call are completed before it exits the IF block?

** , , , , , , , , 50 ELIF, 50 , IF.

!

+4
5

JavaScript Promises. :

JavaScript Promises , . :

$.when(GET_PRODUCTS).then(
 IF_SUCCESS DO THIS
 ELSE DO THAT
).fail(
 SHOW MESSAGE
 CLEAN EVERYTHING BECAUSE SOMETHING WRONG HAPPENED
).done(
 CLEAN EVERYTHING BECAUSE EVERYTHING WENT OKAY
)

, . , , !

+1

,

, , "". , . - ; .

0

, more stuff . , .

"node.js", , . , AJAX-.

, :

var nav = document.getElementById('nav');

function async(params) {
    var xhr = new XMLHttpRequest();

    // set up your request

    xhr.onreadystatechange = function() {
        // some conditions, and then on success:
        nav.style.color = 'black';
    };

    xhr.open('GET', 'resource.php'+params, true);

    // send your request
}

if ( /* condition */ ) {
    async( /* some parameter */ );
} else {
    nav.style.color = 'red';
}

, , #nav , async , #nav , , , , , , , , .

0

, Promises, async.js, step.js .. . , node -harmony.

0

- , .

You do not need to copy / paste N times. Just add “more things” to the function and pass that function to all your asynchronous callbacks. Callbacks can simply call the "more things" function when they are executed with normal processing.

-1
source

Source: https://habr.com/ru/post/1542550/


All Articles