'Fetch/Axios await/async not working inside NextJS API
This is working:
function myApi(req, res) {
fetch(url, opt).then();
}
This not:
async function myApiAsync(req, res) {
await fetch(url, opt);
}
This causes a memory leak:
async function myApiAsyncAlternative(req, res) {
await fetch(url, opt).then();
}
I've to note that I'm making a call no another NextJS API inside this API.
With await the function never reaches the next line. It makes the request, because if for example, I skip the headers, I get a 401 error. Neither working with Axios.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
