title: How to implement concurrency or context-switching in NodeJS
body: <p>So I have this API endpoint called www.example.com/endpoint on which many devices post(I work in an IOT firm). We have implemented our whole backed in NodeJS and are stuck while scaling from 1 device to 'n' number of devices. The devices post their packets at this API endpoint, from where I execute  a complex bit of code(arnd 1000 lines) and save the state of the device in the database(mongoDB). Now the issue is. Whenever I receive a packet from device 1 and I am  executing it and in the middle I get a packet from device 2, NodeJS leaves the device 1 execution as it is and starts serving the packet 2 from device 2, I saw this when I put extensive console.log() statements<br>
  Now in an ideal world. I would want Node to save the context of my current progress with packet 1. then leave. and go on to save the packet 2 in a queue to be processed later. Once I am done with packet 1 I shall take up packet 2 and process it.<br>
  I know libraries like RabbitMQ and kue for storing it in queue and processing it later, <strong>but how do I context switch from one execution to another?</strong><br>
  This is my way of thinking. There could be other solutions as well. Would like to hear your thoughts on the matter.  </p>