-1

I can't understand In which order will the functions foo and bar be executed?

1- foo and bar will execute in the same time, 2- We don't know 3-foo then bar 4- bar then foo, I think 1- but I don't sure

var res = {};

function foo(results) {
    res.foo = results;
}

function bar(results) {
    res.bar = results;
}

// ajax (..) is some arbitrary function given by a library
ajax( "http://some.url.1", foo );
ajax( "http://some.url.2", bar );
  • That depends on when the request gets a response. This depends on many aspects, including how much time the targeted server (behind the URL) takes to produce the response. So there is no predefined order. – trincot Dec 15 '21 at 06:36
  • It depends on the server response time, you can make the calls asynchronous. and the function will be called in the order you call them. – itaynoy Dec 15 '21 at 06:39

1 Answers1

0

The answer is: "How long is a piece of string?"

No, seriously, from what you have posted it is not quite clear whether the ajax() function is a "promise" or a "deferred" object. In either case the behaviour would be that some functionality, as defined in the given URLs would be carried out and after that the bound callback functions "foo" and "bar" would be executed. Nobody can say in which order this will happen.

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
  • 1- foo and bar will execute in the same time, 2- We don't know 3-foo then bar 4- bar then foo, I think 1 but I don't sure – xh9tlf8d82 Dec 15 '21 at 06:53