0

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?

6
  • 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? Commented Jan 10, 2019 at 10:09
  • Checkout this video youtube.com/watch?v=8aGhZQkoFbQ Commented Jan 10, 2019 at 12:52
  • node.js core concepts section provides a great foundational overview nodejs.org/en/docs/guides Commented 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 both Commented 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. Commented Jan 11, 2019 at 7:20

1 Answer 1

1

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
});
Sign up to request clarification or add additional context in comments.

2 Comments

I know about callback chaining and promises. I am confused about what happens when more that one users are accessing my web app. Since Node is single threaded, all the requests from different users will be delievered through this single thread, so is there any way to make sure that if one user wants to make some changes to database then before the completion of this users operations, no other user can change the same entries. I am sorry if I am not very clear, I am very confused about this single threaded nature. Also, my problem is not in handling asynchronous nature but concurrency.
Oh okay I get your point, the concurrency you are talking about is more about the database and must be handled in the way you store and manage the data. For example, when a user is updating some data, the changes are updated only when the API/procedure is called and in the meantime, other users might access the old data. So this can be handled by maintaining timestamps for each update and checking the last updated timestamp to ensure proper data is shown. Hope this helps !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.