Home Examples Docs
9. API calls and integration
Calling backend APIs is simplified in TargetJS through its reactive targets, which activates the next target when the API result becomes available. TargetJS also orders the API results based on the sequence of the API calls made, rather than the order in which the responses arrive, maintaining a strict, code-ordered sequence of operations..
In the example below, we define a target named fetch. It is a special target that performs a GET API call. For more complex requests, you can use the fetch() function within a regular target. The fetch() function also supports caching if needed.
When the API response is received, it automatically triggers the html target, which then displays the user's name.
In the second example, we use the same API but call it five times, appending each user’s data as a separate object in the output. The interval is set to 1000, so a new API call is made every second. We use the fetch() function, passing the user API URL with a separate query parameter for each request.