Question 24
Question
How would you implement a Promise-based API client with retries, timeouts, and cancellations?
Answer
Here's a blueprint for implementing a Promise-based API client with retries, timeouts, and cancellations in JavaScript. This example focuses only on the core concepts. This can be adapted to your specific API requirements:
Explanation:
ApiClient
Class: Represents the client that interacts with your API. It holds the base URL and provides methods for making requests.request()
Method: The core method to handle all API calls.Takes
method
,path
,data
, andoptions
.Merges default retry, timeout, etc., options with any provided by the caller.
Uses
AbortController
for cancellation (more on this later).
Promise Handling:
Returns a Promise that resolves with the API response data or rejects with an error.
Implements retry logic within a loop using
setTimeout
. If retries fail, it rejects the promise.
Timeout: Uses
fetch
's timeout option (signal: controller.signal
) to abort requests exceeding the specified time. You can integrate this with your own timeout handling if needed.Cancellation:
The
AbortController
object allows you to signal cancellation of a request usingcontroller.abort()
.
Example API Calls (
getUsers
,createUser
): Demonstrates how to use therequest()
method for different API endpoints.
Key Points and Enhancements:
Error Handling: Consider adding more granular error handling, logging, and potential fallback mechanisms within your API client.
Rate Limiting: Implement logic to handle rate limiting by the API you're interacting with (e.g., by pausing requests or using exponential backoff).
Caching: Implement a simple caching mechanism for frequently accessed data to improve performance.
Last updated