Script Examples

This section contains examples in the form of code snippets to illustrate how the Script Function can be used.

Example - Tracking Consumed Volume

To keep track of the consumed volume of this Function:

state.node.consumedVolume = state.node.consumedVolume || 0; const shouldPush = (++state.node.consumedVolume % 10) === 0; if (shouldPush) { log.info("Reached chargeable volume, pushing event for charging"); // volumeDiscount is updated by another function in the stream await push({ charge: true, volumeDiscount: state.stream.volumeDiscount }); } else { log.info('Consumed volume is ${state.node.consumedVolume}'); }

Example - Using Store in Tranform and Flush

In Transform

To set the latest call ID to the payload call ID:

await store.set('latestCallId', JSON.stringify(data));

To fetch the value of the latest call ID:

const result = JSON.parse(await store.get('latestCallId'));

In Flush

To delete the latest call ID at the end of each transaction:

Example - Set a Timeout Value

You can set a timeout value by stating it to the system. In this example, we will set it to 3 seconds and then deployment will be triggered. 

Example npm Libraries

Below are a few code examples of how you can use npm libraries.

For further information on JavaScript, see https://devdocs.io/javascript/ .

For an example of how to use Script in a stream, see My First Stream.