5.7.3 Using the Macro API

In Host Access for the Cloud macros are recorded and written using JavaScript. JavaScript is a popular and prevalent programming language. There are a wide variety of learning resources and tools available to you.

The Macro API consists of a set of objects which you can use to interact with the host, wait for screen states, and interact with the user.

About promises and yields

Because JavaScript is single threaded and uses 'callback functions’ and ‘promises’ to help manage the flow of execution through code, often code can be difficult to follow. Host Access for the Cloud combines the concept of ‘promises' with the ‘yield' keyword so macro code can be organized in a more linear fashion.

  • Promises

    Promises are patterns to help simplify functions that return their result asynchronously, at some point in the future. All ‘wait’ and ‘ui’ functions in the Macro API return promise objects.

  • Yield

    Macros use the yield keyword to block the execution of the macro until a promise is resolved, or done. So putting yield in front of any ‘wait’ or ‘ui’ function makes the macro execution pause until that function has finished executing. You can place the yield keyword in front of any function that returns a promise, even your own custom functions.

NOTE:The ability to make macro execution block by combining yield with promises is enabled by the createMacro() function.

Errors

Errors are handled in macros using a try / catch statement. Some API functions may throw errors if, for example, conditions can’t be met or a timeout occurs. The thrown error is ‘caught’ in the catch statement. You can wrap smaller blocks of code in a try / catch statement to handle errors at a more granular level. Macro developers can also throw errors with 'throw new Error('Helpful error message');

Related Topics