Asynchronous calls are an inherent part of javascript, and using callbacks is often an elegant tool to handle these calls.
However, I am not quite sure how is the branching of code following an asynchronous operation decided. For example, what would happen with the following code?
function f(callback) {
value = some_async_call();
if (value) {
callback(value);
}
return(value);
}
What would happen here? From my short JS experience, return would send back an undefined value. But suppose that value returns true from the asynchronous call, would the callback be called with the right value or with an undefined value?
In other words, is there a rule regarding which operations are executed immediately after the async call, and which are deferred until the value is returned?
What have I tried before asking
SFTW for branching asynchronous calls in javascript, but found nothing canonical or decisive.
via Adam Matan
No comments:
Post a Comment