How to call multiple rest calls in parallel inside a ScriptInclude
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-31-2015 07:25 AM
I'm very new to service now development and I am creating a custom application that needs to make rest calls to our backend systems.
I am able to make the rest calls from an Ajax Handler that I have in ScriptIncludes that extends the AbstractAjaxProcessor.
On one of the custom pages I created I want to make an Ajax call when the page is being initialized that will handle getting the initial data for the page using an Ajax request. I am doing it this way in order to get the page to display as quickly as possible and not have it be held up while I am getting some initialization data from our backend.
My issue is that I want the handler function for this that lives in my ScriptInclude that extends from the AbstractAjaxProcessor (as mentioned above) to be able to make multiple rest calls asynchronously and then when all the calls return send a response back to the caller.
Is this possible?
I am currently doing something like this:
var r = new sn_ws.RESTMessageV2('x_name_here', 'get');
r.setStringParameter('x', x);
r.setStringParameter('y', y);
r.setStringParameter('z', z);
var response = r.execute();
This seems to be synchronous.
First of all is there a way to do this asynchonously?
Is there a way to make multiple calls asynchronously and wait until they are all done?
In node or in Angular on the client side, I would use a promise library, is it possible to include a third party library that a script include can use? If so how?
Thanks for your help with this!
...Ed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-31-2015 01:43 PM
Hi Ed,
You can use r.executeAsync(); But then script would block when you access response object.
There is a way to have multiple calls run parallel. You can create an event and perform the actual processing in the Script Action associated with the event. And wait for all the events to be processed.
Regards,
Siri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-03-2015 08:11 AM
Thanks Siri,
This is helpful. Its more than I'd like to do for my proof of concept at this time, so I think I am going to handle this client side for now.
...Ed