Is it possible to make Javascript's fetch() wait for response, even if response takes an hour to return, without making a second request attempt?
Clash Royale CLAN TAG#URR8PPP
Is it possible to make Javascript's fetch() wait for response, even if response takes an hour to return, without making a second request attempt?
On frontend (html file) I have a JS code which launches a POST request to node server,some processing takes place at the server end and after that the response is sent back.
The processing can take upto like half an hour also. But what happens is after just 5 minutes of not receiving response it resubmits a POST request, which is not what I want as it'll interrupt the backend processing.
My question is, is it possible to make this happen? If yes how?
Here's my fetch code
fetch('<url_here>',
body: JSON.stringify(data),
headers:
'content-type': 'application/json'
,
method: 'POST'
)
.then(response => response.json())
.then(res =>
//work with response data
);
That sounds reasonable.
– Tripti Rawat
Aug 10 at 10:04
basically, delegate a task to the server, then either use a socket to pull the client once the sever is done (you may use socket.io for such), otherwise, just keep polling the server (from the client) until the task is effectively finished. Also, remember to start the task asyncronously, possibly in a separate thread or whatever (by eventually handling a queue).
– briosheje
Aug 10 at 10:07
2 Answers
2
Apparently, Fetch does not natively implement a way to set a timeout. Some people have developed workarounds.
However, I'm not sure this is the way to go. If your operation takes an hour and a half and you want to get the result whenever it is done, keeping your browser open and untouched for that long is complicated. What happens if the user closes the tab then reopens it, or refreshes the page? It will make a second call.
IMO the way to go is using websockets (socket.io) so the server can notify the client(s) whenever it's ready, and also send data along, even if the page has been refreshed in the meantime.
You can't.
If you need to do a lot of processing on the server, then break it up into parts.
There are a few ways you could implement step 6. The simplest is to have the client poll, on an interval, the server with "Is the data for my id ready?". An alternative would be to use Web Sockets and push the response to the client when it is ready. If you want to wait for up to 90 minutes, then looking at Web Workers and the Notifications API would probably be wise too.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I don't think you should ever have a server task taking that much time to complete (unless it's scheduled, then that's fine). Is there literally no other alternative other than forcing the client to wait for a response? I would rather delegate the process by performing a request, starting a task, returning that the task has started and poll the server each X seconds (let's say 1 minute?) to know whether the scheduled task is finished or not.
– briosheje
Aug 10 at 10:03