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

20 Replies, 1248 Views

For rest api request javascript, async/await is your friend.

```javascript
async function getData() {
try {
const res = await fetch('url');
if (!res.ok) throw new Error('Failed');
const data = await res.json();
return data;
} catch (err) {
console.error('Boom:', err);
}
}
```
Always wrap in try/catch. And yeah, Axios is way more intuitive.

Messages In This Thread



Users browsing this thread: 1 Guest(s)