One thing that we haven't touched on so far, which is interesting to consider, is the fact that promises can be chained. This is especially useful to avoid code duplication.
In our Fetch example, we have called an API, checked the status code, retrieved the body and parsed it as JSON. Here's another way to implement the same functionality but in a more reusable manner:
function checkResponseStatus(response: Response): Promise<Response> { if (response.status >= 200 && response.status < 300) { return Promise.resolve(response); } else { return Promise.reject(new Error(response.statusText)); } }
function toJSON(response: Response): Promise<Response> { return response.json(); }
const responsePromise: Promise<Response> = fetch('https://api.worldbank.org/v2/countries?format=json...