Sync issue between device and agent

I have the following mechamism:

  1. app send post req
  2. agent reads req
  3. agent passes req to device with device.send(…)
  4. device does short process and updates a string on the agent with agent.send(…)
  5. agent sends back to my app a response res.send(200,msg)
    msg is the string the device updates, it is stored on the agent.
    in my agent i have this code:
    http.onrequest(function(req, res) { if (req.method == "GET") { res.send(200,msg); server.log("GET"); } if (req.method == "POST") { local arr = split(req.body,";"); device.send("setParams",arr); res.send(200, msg); server.log("post"); } server.log(req.body); });

only the POST part is relevant for my question.
the second line sends the array to my imp, which, as fast as it can, returns the msg (even before actually making the changes…) but the agent is a separate entity, so it continues and sends the response right after.
Is there a way to make it blocking somehow? so that it will wait for the answer from the device?
the last option would be parse the request on the agent, but that’s like doing useless work for nothing.

See the code here: http://devwiki.electricimp.com/doku.php?id=electricimpapi:http:onrequest

You need to save the res, and only do res.send() when the device.on() triggers with the returned value from the imp. The two last code segments on that page do exactly what you’re looking for.

Thank you. I didnt know i can save the response like any other object :slight_smile: