2

Lets say i have a method updateRecord(), and this method performs some async requests inside of it, and some of this requests are more important than others since they impact hardware, and some are, although necessary, only affecting a record in a db. All this info combined represents one action for the business model.

What should be the order of this requests, regarding fiability of data? Should i do all the non important first (that will probably not fail) and then the hardware one (can fail to update since it requires internet conection, but the value saved here is the only important value at the end of the day)? If the important one fails, should i delete the records created before?

The other way around?

Or some other way?

Some pseudocode to represent my dilemma:

async updateRecord(){
  a = await setLogOfCurrentData()
  if (a) { do something }
  b = await updateARecordInDB()
  if (a) { do something }
  c = await updateImportantInfoInHardware()
  if (!c) { 
    ??? 
  }
}

Thank you!

Ps: dont know how to properly tag this question, if you have some ideas let me know!

2 Answers2

1

The order probably doesn't matter. It would matter if:

  • One or more of the requests requires a lot of bandwidth (either download or upload), such that the amount of data to be transferred would interfere with other requests. In such a case, if it's a less important task, you'd probably want to either defer this expensive request until the others are complete.
  • Or, if you have lots and lots of requests, and the endpoints are to servers using HTTP 1.1, it would be better to send out the most important requests in small batches first, and only send out later requests once they're done. This is because, on HTTP 1.1, browsers/operating systems throttle large numbers of concurrent requests.

In your case, if the requests are independent - that is, if the do something blocks aren't related to each other - and you aren't using a lot of bandwidth, then the better approach would be to use Promise.all or Promise.allSettled so that they can all finish quicker, without having to wait on the previous request to complete.

function updateRecord() {
  return Promise.allSettled([
    setLogOfCurrentData(),
    updateARecordInDB(),
    updateImportantInfoInHardware(),
  ])
    .then(([logResult, dbResult, hardwareResult]) => {
      // do stuff with results
      // those with errored will be of the form
      // { status: 'rejected', reason: SomeErrorObject }
    });
}

Promise.allSettled is probably the best approach here, since that way you can examine each result individually.

(Or you could use Promise.all and put .then or .catch handlers onto the end of each Promise, eg setLogOfCurrentData().then(handleLogResult))

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

Sorry if I understood your question incorrectly.

I think you are saying that you would like to rollback the changes if this important request fails?

If that is the case, then what about something like this:

async updateRecord(){
  // Make this important request first
  c = await updateImportantInfoInHardware()
  if (!c) { 
    // throw error here
  }

  a = await setLogOfCurrentData()
  if (a) { do something }
  b = await updateARecordInDB()
  if (a) { do something }
}

this way if the most important request fails then you won't have to worry about rolling back other requests

Vin_it
  • 186
  • 1
  • 5