WebPages JavaScript Promises

In JavaScript, the Promise notation is used in computation of asynchronous functions. A Promise can be in three states: pending, fulfilled or rejected. It can take two arguments: a results handler and an error handler either will be called only once and will return a value after the asynchronous function has finished. This allows for chaining multiple calls which will execute sequentially. It is also required when the result of a function is needed, for example when assigning the result to a variable as in the example below.

Example: When assigning the result of an asynchronous function to a variable, use the .then() syntax: 

$System.GetUptimeMilliseconds().then(function(result){$mytime_test = result});

Instead of:

$mytime_test = $System.GetUptimeMilliseconds();

Example: Error cases can also be handled by specifying a second argument with a callback function:

asyncCallToDesigner(parameters).then(function(result) {
	//handle result here	
},
function(error) {
	//handle error here	
});

                                    
Note:
  • When handling error callbacks, a JavaScript error object will be passed. For more information, see the Error topic (requires Internet access).
  • The then callback is not required; it is only needed to handle the result of an asynchronous call. In addition, within the then, error handling is not mandatory.
  • The parameter names in the function callbacks can be arbitrary names, not just result or error. Parameters are not mandatory; you can run a function, instead.