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: 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 });
- When handling error callbacks, a JavaScript error object will be passed.
- 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.