I know how to wait for ajax to complete, but if I'm going to redirect a page after some ajax calls are fired off, should I wait for them to complete before the redirect? Does it matter?
If you're confident that the ajax call will be successful, then once the ajax call has occurred, redirecting won't affect it as far as your server is concerned. But don't forget, the client could lose their connection or a number of errors could occur, so you should probably wait to make sure the calls were successful. Another thing to consider is whether or not your ajax calls will affect whatever page you're going to redirect to. In that case DEFINITELY wait before redirecting. You won't want your user to get redirected to a broken page because their connection was slow or your server had a hiccup.
I recommend waiting and handling possible errors that might occur.
You gain safe connection, error spotting, better software, by doing the whole process synchronously, making the redirection coming in the end.
You might spend 0.1 second more of execution but who cares?
I would argue that Ajax followed by a redirect is not a good use case for Ajax. Instead I would recommend doing a regular server request and then take care of all operations on the server before redirecting to your final destination.
This will make for a simpler pattern and a lot less network traffic. I recommend to use Ajax in cases where you want to avoid reloading the page. However in your scenario, you are planning on leaving the page as soon as your operations have completed, so why not avoid the back and forth that Ajax will give you.
© 2022 - 2024 — McMap. All rights reserved.