Reputation: 2247
How do I chain qbo3 API calls as promises in Javascript?
I’m trying to get the JavaScript below to pause after hitting var api
until var data
has been completed. Then after being completed, the function can continue on as usual. The overall goal being to get an approx. measure of how long it is taking for the query to run after being called on through the function call.
var data = {...};
var api1 = new qbo3.ProcessObject();
api1.invokeJson('StartRecordingTime',data);
var api2 = new qbo3.ProcessObject({target: 'row_4'});
api2.invokeHtml('RealWorldApiCall', data);
var api3 = new qbo3.ProcessObject();
api3.invokeJson('EndRecordingTime');
Upvotes: 0
Views: 54
Reputation: 2247
You can construct a promise based on a qbo3 API call as follows:
qbo3.getPromise = function(cn, method, data) {
return new Promise(function(resolve, reject) {
new (qbo3[cn] || qbo3[cn + 'Object'])().invokeJson(method, data, { success: resolve, error: reject });
})
}
where:
cn
: is the className of the API endpoint (e.g. Process
, Loan
, Person
, Message
, etc.)method
: is the class method to be invoked (e.g. Select
, Search
, Update
, etc.)data
: is the JSON
data to be submittedThe
(qbo3[cn] || qbo3[cn + 'Object'])
expression is just a bit of sugar to allow you to pass eitherProcess
orProcessObject
as thecn
parameter toqbo3.getPromise(...)
.
Then you can use:
var data = {...}
var myPromise = qbo3.getPromise('Process', 'StartRecordingTime', data)
.then(return qbo3.getPromise('Process', 'RealWorldApiCall', data))
.then(return qbo3.getPromise('Process', 'EndRecordingTime', {}))
More generic usage leveraging the results of one API for the data being passed to a subsequent call:
var somePromise = qbo3.getPromise('Person', 'Search', {"Person": "[email protected]"})
.then(json => {
const id = json.PersonCollection.PersonItem[0].PersonID;
return qbo3.getPromise('ProcessObject', 'Search', { "CreatedPersonID": id })
}).then(processes => console.log(processes));
Note that if you're just troubleshooting response times, qbo3
includes an X-Execution-Time
response header all API calls. This header is not passed to the resolve
method, you can inspect it from Chrome's developer console's Network
table, pictured here:
Upvotes: 0