Is there any way I can assign my task to other server/thread

16 Views Asked by At

I am using NodeJS and Google Cloud Console. I have a post-API app.post("/user", middleware) This post API updates user details in the DB. The req.body also contains coordinates[lat, long] I already have the logic to fetch the address based on coordinates, suppose we have a function fetchAddrs() which fetches the address and then updates the user.location in DB. Now I want to perform this task separately which means I don't want my current or primary server to be blocked by this operation. I want that somehow fetchAddrs() task run separately on another server or threads and update the user DB.

Sorry if you didn't understood my question explanation. I just want my function to execute separately thereby not blocking my current server. I think it may be done through jobs or scheduling but I dont have any exact answer.

This is how my code looks like.

function fetchAddr(lat, long){
  // fetches address from external api
  updatesUserLocationInDB(res);
}

app.post("/user", function(req, res){
  // logic to update user db with data in req.body

  fetchAddr(lat, long) // I want this task to be done separately
  res.json(user.data)
  
})
0

There are 0 best solutions below