What's the best way to make a REST API request in JavaScript? or How do I properly handle a REST API

20 Replies, 1299 Views

ugh, fetch() is a pain sometimes. One thing that got me: you gotta chain .then() properly or it won’t work.

Also, if the API is down, your code *will* crash unless you handle errors. Wrap it in try/catch if using async/await:

```javascript
try {
const response = await fetch('your-api-url');
const data = await response.json();
console.log(data);
} catch (err) {
console.log('RIP API', err);
}
```
Check out the MDN docs on rest api request javascript—super helpful!

Messages In This Thread



Users browsing this thread: 1 Guest(s)