JavaScript API Why does client.search method always execute last?

I'm using the client.search method very similar to the example with the .then in it. For some reason, it seems to always execute after all other operations in my code (save some global variable declarations at the start) that come after the client.search method, even if I put in a delay between them (to rule out any asynchronous race conditions).

  1. Invoke the client.search method
  2. (optionally add a sleep.sleep(10); delay which hasn't helped)
  3. Handle or console.log(outputFromClientSearch)

Putting the delay between steps 1 and 3 results in things executing in the order of 2 -> 3 -> 1. Without the delay this executes 3 -> 1.

The only way to print or handle the output from the client.search method is within it, generally within the .then section. I have also tried encapsulating 1, 2 and 3 in functions and calling them in sequential order, still they execute in the same incorrect sequence. This greatly limits what can be done from a handling standpoint on the results of a query.

Is there a way to ensure the order of operations here?