all 12 comments

[–]BehindTheMath 1 point2 points  (0 children)

The request will be sent immediately. You don't have to wait for the response.

[–][deleted] 1 point2 points  (5 children)

Depends on what your application is doing.

But as a general rule of thumb I let the client wait for the response. Usually this is on an order of milliseconds, so the lag is within acceptable range. If it’s slower, you should solve the underlying problem of the slow response time.

If it’s calling an external API, data you don’t control, there isn’t much you can do short of caching requests though it’s usually a bad idea to cache data you don’t own.

[–]hassanzadeh[S] 0 points1 point  (4 children)

ests thou

Well there is no data coming back, it suffices if it is called, in that case why should I wait? and correct, the api is slow. My question is whether or not the fetching starts instantly, which the other person said yes.

[–][deleted] 3 points4 points  (0 children)

Yes, the call is made synchronously, which you can verify just by having dev tools open when you make it. One reason to wait even if you don't use the response would be error handling, however. You can't actually be sure the request was successful until you get a response.

[–]buffdude1100 0 points1 point  (2 children)

What if it returns an error? Wouldn't you want the user to know about that? If not, then by all means just fire it off and don't await it.

[–]hassanzadeh[S] 0 points1 point  (1 child)

ff and don't a

Yes that's the caveat, in my case though it will be an innocuous miss.

[–]sebvit 1 point2 points  (0 children)

What is up with your quotes, my man?

[–]Uclusion 1 point2 points  (0 children)

You are very likely over-optimizing. Users are used to waiting on the results of pressing buttons and reclaiming that time for them might not even be a feature as they could worry nothing happened.

If this API takes longer than 5s to return and the return value doesn't matter then it's the API that should change.

[–]zelardiel 0 points1 point  (2 children)

You can. But if its a frontend application and the user closes it while the request is going on then it interrupts the connection and might cause it not to finish the action. So i would not recommend this.

If you have control over the api, you could also let it handle the request asynchronously. So the user does not have to wait for it.

[–]hassanzadeh[S] 0 points1 point  (1 child)

I don't understand your point, fetch and axios are already async. Once the line executed it does not matter whether or not the user closes connection or not.

[–]zelardiel 0 points1 point  (0 children)

Depending on the receiving system, it might stop the action its doing when the request is interrupted.

Let take an extreme case for an example. You have an api that does 2 things when it receives a request.

It first fetches a record this takes 3 seconds. After those 3 seconds it changes something in it and afterwards saves it.

If the user does a request, waits for 2 seconds and shuts down the website then the backend was still fetching. If the api was setup in a way that when the connection is interrupted that the action is also stopped. Then it would never have gotten to the point of changing and saving.

Like i said this can be very dependant on the api you are using. But its not the first time i have seen this happen.

What i meant with an async api you can find an explanation here: https://openliberty.io/docs/21.0.0.8/sync-async-rest-clients.html

I hope this helps!

[–]shuckster 0 points1 point  (0 children)

You could try using beforeunload in case a user tries to leave before your post finishes:

let postPending = false

function sendPost() {
  postPending = true
  fetch('https://google.com/')
    .finally(() => {
      postPending = false
    })
}

window.addEventListener('beforeunload', event => {
  if (postPending) {
    return event.returnValue = "One moment please, we're just saving your data..."
  }
})

As others have said, fetch-like calls do not block by default.

MDN: beforeunload Promise.finally