Is it possible to make parallel asynchronous call using Javascript httpclient with callback (without using waitForComplete())? The requirement is to call multiple requests (up to 5) & mashup the result. We developed a for proxy for this in 2016 using Javascript httpclient which uses waitForComplete(). In preflow request we make multiple requests like below.
searchRequests.forEach(function(searchTypeValue) {
var searchTarget = callbackUrl +"/documents/search"+"?"+searchTypeBackeEndProp+"="+searchTypeValue;
var eventReq = new Request(searchTarget, "GET", headers);
var exchange = httpClient.send(eventReq);
searchObject.push({"calloutObject": exchange,"searchTypeValue":searchTypeValue, "searchTypeRequestProp":searchTypeRequestProp, "searchTypeBackeEndProp":searchTypeBackeEndProp, "fieldName":fieldName});
});
context.setVariable("searchObject", searchObject);
And in post response flow, we assemble responses something like below
var searchObject = context.getVariable("searchObject");
var searchRecords=[];
for(var key in searchObject){
if(searchObject.hasOwnProperty(key)) {
var search = searchObject[key].calloutObject;
var searchTypeValue = searchObject[key].searchTypeValue;
var searchTypeRequestProp = searchObject[key].searchTypeRequestProp;
var searchTypeBackeEndProp = searchObject[key].searchTypeBackeEndProp;
var fieldName = searchObject[key].fieldName;
searchRecords[key]=getsearchRecords(search, searchTypeValue, classValue, searchTypeRequestProp, searchTypeBackeEndProp,fieldName);
}
}
var searchResponse = {};
status = "true";
searchResponse.status = status;
searchResponse.searchRecords = searchRecords;
response.headers['Content-Type']='application/json';
context.setVariable("response.content",JSON.stringify(searchResponse));
this.getsearchRecords = function(exchange, searchTypeValue, classValue, searchTypeRequestProp, searchTypeBackeEndProp, fieldName) {
var searchRecordsValue={};
var respContent;
exchange.waitForComplete();
if (exchange.isSuccess()) {
//Construct response and the result
}
}
As per this link waitForComplete is anti pattern - https://docs.apigee.com/api-platform/antipatterns/wait-for-complete
Is it possible rewrite this in java script without using waitForComplete? I think async/await or Promise doesn’t work in Apigee JS policy.
Note: I am not looking NodeJs (hosted target) solution due to Apigee limits (max 20) per org.