The GatewayScript to look at is in GatewayScript_policy.js
- Run
service.js
on a server your DataPower server can access. This sets up a simple web service on port3003
with a deliberately slowed response. - Create a
Set Variable
policy to setpromisedservice
to the server you set upservice.js
on.
1. Set up a `GatewayScript` policy, and paste in the source of `GatewayScript_policy.js`.
Your policy chain should look like this:
The Source's assembly
will look very much like that of
this Open API spec
GatewayScript uses the standard ECMAScript 6 Promises.
service = apim.getvariable('promisedservice')
This retrieves the value set by the Set Variable policy.
// start the "a" request, which will respond in 200ms
var promiseA = new Promise((resolve, reject) => {
httpGetRequest(service+'/a', resolve, reject);
});
// start the "b" request, which will respond in 400ms
var promiseB = new Promise((resolve, reject) => {
httpGetRequest(service+'/b', resolve, reject);
});
This creates two promises. A promise represents data that isn't
available yet. At a later time, a promise can be either
resolve
d (data is now available) or reject
ed (an exception
occurred).
Promise.all([promiseA, promiseB]).then(values => {
. . .
}).catch(error => {
. . .
})
This waits until either all promises are resolved, or at least
one promise is rejected. The values
parameter is an array with
the resolve
data from each promise. The error
is the reject
data from the promise that failed.