-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Recursive fetch doesn't work on server-side #5168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
If you want server-side prerendering to wait for your chain of promises to complete, then you must add the outer promise to the list of domain tasks, like the For example, import { fetch, addTask } from 'domain-task'; ... and then... const myTask = fetch('https://jsonplaceholder.typicode.com/posts/1')
.then(response => response.json())
.then(response => {
return fetch('https://jsonplaceholder.typicode.com/posts/2')
.then(response2 => response2.json())
});
addTask(myTask); // Here's where we register this task so that prerendering waits for it
return myTask; This should keep the domain context open for as long as your promise chain is in flight, so it should not clear out the |
I was aware of |
@SteveSandersonMS - I have published example that shows this issue. To reproduce that:
|
@rosieks Thanks - I'll check it soon. Reopening. |
@SteveSandersonMS Would you mind making Great use case for using async await too, although not sure on the projects policy on that. |
I just made another test - I replaced first fetch with: Maybe replacing it to async/await will help? |
any workaround for this? it looks like when nested fetch is called there is no active domain and baseUrl returns undefined. |
Sorry this never got a timely response. I think the behavior reported here comes down to a Node issue whereby it fails to preserve domain context in native Promise continuations. That's not something we can directly fix, so if you're chaining promises and using @mkArtakMSFT Propose we close this as it's an external (Node) bug that we're not planning to work around. |
I tried to run such code on server-side but for some reason it doesn't work.
fetch
function is imported fromdomain-task
I receive the following error:
The text was updated successfully, but these errors were encountered: