Skip to content

capnajax/GatewayScriptPromises

Repository files navigation

GatewayScript promises example

The GatewayScript to look at is in GatewayScript_policy.js

Setup

  1. Run service.js on a server your DataPower server can access. This sets up a simple web service on port 3003 with a deliberately slowed response.
  2. Create a Set Variable policy to set promisedservice to the server you set up service.js on.
the set-variable policy that sets promisedservice to your server's url

1. Set up a `GatewayScript` policy, and paste in the source of `GatewayScript_policy.js`.

Your policy chain should look like this:

policy chain with a 'set-variable' and a 'gatewayscript' policy

The Source's assembly will look very much like that of this Open API spec

What it does

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 resolved (data is now available) or rejected (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.

Resources

ECMAScript 6 Promises (Mozilla)

GatewayScript Code Examples

This Open API spec

About

Example of how to use promises in DataPower GatewayScript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published