Announcements
The Google Cloud Community will be in read-only from July 16 - July 22 as we migrate to a new platform; refer to this community post for more details.
Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

HTTP requests from a JS callout are not really behaving Asynchronously?

Not applicable

Team,

I was following this link and implemented Asynchrous HTTP Client Callout using JS approach.

https://community.apigee.com/content/kbentry/2340/asynchronous-http-requests-in-an-api-proxy.html

My Requirement:

I need to make 3 service callout asynchronously and get their response object - do mashup and give one single output. so i implemented HTTP service callout using JavaScript approach , but the results doesnt look its really asynchronous.

(I am making 3 JS Service callout under 1 JS file and storing the individual service response in session.context for mashup)

var calloutResponse1 = httpClient.get('http://httpbin.org/getService1');
calloutResponse1.waitForComplete();
context.session['calloutResponse1']= calloutResponse1;
var calloutResponse2 = httpClient.get('http://httpbin.org/getService2');
calloutResponse2.waitForComplete();
context.session['calloutResponse2]= calloutResponse2;
var calloutResponse3 = httpClient.get('http://httpbin.org/getService3');
calloutResponse3.waitForComplete();
context.session['calloutResponse3']= calloutResponse3;

Result::

Service1_Call Start Time :: Fri May 13 2016 11:15:40 GMT-0400 (EDT)

Service1_Call End Time :: Fri May 13 2016 11:15:46 GMT-0400 (EDT)

Total Time Taken by Service 1 =5674 msec

Service2_ Call Start Time :: Fri May 13 2016 11:15:46 GMT-0400 (EDT)

Service2_ Call End Time :: Fri May 13 2016 11:15:47 GMT-0400 (EDT)

Total Time Taken by Service 2 =1009 msec

Service3_ Call Start Time :: Fri May 13 2016 11:15:47 GMT-0400 (EDT)

Service3_Call End Time :: Fri May 13 2016 11:15:47 GMT-0400 (EDT)

Total Time Taken by Service 3 =34 msec

Though the Service1,2,3 Call Start Time is close - Parallel. the overall response time from this JS callout is

5674 +1009+34 =6717 mseconds instead of 5674 mseconds..

I was expecting this in Asynchronous flow

1. All 3 service callout should start at the same time..

2. The overall response time should be the response time of the max service response time & not sum of 3 service response time.. 5674 msec is what i am expecting here.

Please advice

Solved Solved
3 21 2,877
1 ACCEPTED SOLUTION

ceber
Former Googler

You need to move the waitForComplete calls to another policy. That's the call that forces the javascript execution to wait for a response. So you are absolutely right, you won't see asynchronous behavior this way. So yank those statements out of this policy, fire off the requests and store them in context, then in a second policy assemble the responses.

View solution in original post

21 REPLIES 21