Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 142591

Proper way to handle race condition for 3 microservices. (Promise.all ?)

$
0
0

How sure I am that promise.all will run the promise request one by one?

Scenario I have 3 services. frontend, uploader, and apifrontend is the client, uploader handles google storage related actions, and api handles normal REST stuff.

On my database I have something like:

{
  id: 1,
  directory: 'googlestorage/dir/randomstring.jpg',
  user_id: 100
}

Now in my frontend, I have a function that triggers an update to the directory and deletes the old one so the end result should be.

{
  id: 1,
  directory: 'googlestorage/new_directory/anotherRandomString.jpg',
  user_id: 100
}

In my frontend I need to make sure that the call to the api to update the record on the database should go first before deleting it by calling the fileuploader.

but on my fileuploader I pass the directory to check and validate whether the user is allowed to delete the file or not.

But If I update the database first, then I won't be able to check if the record is still on user's because the data is already updated and will not match the old one.

I also don't want to delete the file first because what if the request to update the database fails?

I tried doing:

await Promise.all([requestUpdateToApi, requestToDeleteFileUploader]);

But it's the same, one will be updated first or will be deleted first before the other.

I wanna do it so if one request fails, the other should not proceed too - is there a good solution for this without touching the database structure?


Viewing all articles
Browse latest Browse all 142591

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>