A link you provided in comments offers some insight into this invokeJson
operation. Specifically this bit here:
invokeJson(method, data, { success: resolve, error: reject })
Those success
and error
properties on that third method parameter are callbacks. Similar to the often seen jQuery AJAX implementation, they essentially are where you would create functions that will be invoked at a later time when the operation finishes (or fails).
Since your code is invoking 2 of these operations, you effectively have three "moments" you can debug. Before the first operation, between the two operations, and after the second operation. Using the tools you have that might look something like this:
console.log('Before first operation.');
var api = new qbo3.ProcessObject();
api.invokeJson('StartTime', data, {
success: function () {
console.log('Between the operations.');
var api2 = new qbo3.ProcessObject();
api2.invokeJson('EndTime', null, {
success: function () {
console.log('After second operation.');
}
});
}
});
As you can see, each call to invokeJson()
includes a third parameter with a success
callback function, which will be invoked after the asynchronous operation completes.
Now, to your question, specifically:
How to make parts of a code wait until another part has completed using async-await
The library you're using doesn't seem to make use of async/await. However, the answer you linked to in comments shows how to get around that. You'd wrap the operation in a Promise
, the structure of which includes resolve
and reject
functions which do exactly what the success
and error
callbacks do.
Slightly modifying the code in that answer, you can create a function which accepts one of your api
objects and returns a promise around invokeJson
. Perhaps something like this:
async function invokeJsonAsync(api, str, data) {
return new Promise(function (resolve, reject) {
api.invokeJson(str, data, { success: resolve, error: reject });
});
}
Now instead of calling invokeJson
directly on your api
objects, you'd pass them to this function and await
the returned Promise
:
var api = new qbo3.ProcessObject();
await invokeJsonAsync(api, 'StartTime', data);
var api2 = new qbo3.ProcessObject();
await invokeJsonAsync(api2, 'EndTime');
Note that you'd also need to use the async
keyword on your Searh_09
function definition in order to use await
within that function.
The linked answer takes it a step further by making this function part of the api
objects themselves. Which would be cleaner if you expect to use this a lot. The programmer appears to have done that with the intent of using .then()
to chain these calls together. Using await
has the same effect, and is specifically what you were asking for.
Overall, it's worth understanding that this isn't "making parts of the code wait until other parts complete". Code does that by default. It imperatively executes line by line until it completes.
However, some of the operations invoked by code these days are asynchronous. Which means the context of performing that operation is offloaded to another process outside of the code that's running. The code that's running will continue to execute more statements. At a later time, when that asynchronous process completes, it will transfer control back to this code for any potential follow-up.
Since JavaScript is single-threaded, any follow-ups to these asynchronous operations will happen after all of the imperative code statements have been executed. The follow-up operations which later get invoked are these callback functions. (Hence the name... It's a function that will be invoked when the control is returned to the code. The other process is using this function to "call us back".)
async
and await
simply wrap this in cleaner syntax. But ultimately what's being "awaited" is a Promise
which has callback functions. When you use await
you're basically telling the code that everything after this statement in the current function is the callback.