I am fairly new to nodejs? My question is how does nodejs handles concurrency exactly? I know that nodejs is single threaded and doesn't wait for requests to complete but does that mean we don't have any control in the order of events that occur?
-
Since events occur not randomly but in some specific order, we may have control over them. Async control flow is implemented by several proven recipes - callbacks, promises, etc. The question cannot get a good answer because it's too broad. Do you have some case in mind? What are exact 'events' you talk about?Estus Flask– Estus Flask2019-01-10 10:09:25 +00:00Commented Jan 10, 2019 at 10:09
-
Checkout this video youtube.com/watch?v=8aGhZQkoFbQshamon shamsudeen– shamon shamsudeen2019-01-10 12:52:05 +00:00Commented Jan 10, 2019 at 12:52
-
node.js core concepts section provides a great foundational overview nodejs.org/en/docs/guidesdm03514– dm035142019-01-10 14:32:36 +00:00Commented Jan 10, 2019 at 14:32
-
@estus Actually I wanted to make a movie booking system, so I have to check if a seat is booked or available in database. If a seat is available then I have to write the seat number in the users bookedSeats field in database and changes its status to booked. So, I am confused that if two users execute the function at the same time, and since node is single threaded if the request from one user confirms that the seat is available and before the same seat's status is changed to booked, the second user also checks that it is available, then the seat will be booked by bothSaurav– Saurav2019-01-11 07:04:46 +00:00Commented Jan 11, 2019 at 7:04
-
Then you need to ask specific question with your current implementation. See stackoverflow.com/help/how-to-ask and stackoverflow.com/help/mcve . The case isn't specific to Node. It would be the same with any web app. It's primarily DB problem. You need to lock DB if possible before writing to it, check that the state isn't 'booked' yet and return an error in case it was. Something like set state="booked" where seat=27 and state!="booked" DB query pseudocode.Estus Flask– Estus Flask2019-01-11 07:20:19 +00:00Commented Jan 11, 2019 at 7:20
1 Answer
Carrying out tasks in an asynchronous manner is one of the main strengths of NodeJS. Essentially, your program never stops and uses a single thread and performs operations asynchronously, thus the way you program in NodeJS will be quite different from traditional C/C++.
Most of the operations which need time to process, such as API calls will be implemented using callbacks, promises. As you start coding, you will understand and get used to it. If you need control over the order of events, especially in cases where the events are interdependent, async is a great package that offers various options to carry out tasks in a serial, parallel, auto, waterfall and many other ways ( You can refer to the documentation at : https://github.com/caolan/async )
Here's an example of performing operations serially using the async package :
async.series([
function(callback) {
// Perform an API call
callback(null, 'one');
},
function(callback) {
// do some more stuff ...
callback(null, 'two');
}
],
function(err, results) {
// reach here after the functions 1 and 2 are completed
});