1

JavaScript is a single threaded programming language (can do one task at the time)

So it first runs the main thread (synchronous) then executes asynchronous tasks in the event loop (this is how I understood but apparently I'm wrong)

Node.js uses libuv a library which handles asynchronous tasks

// synchronously open file descriptor var fd = fs.openSync('testFile', 'r+'); // synchronously write data to the file fs.writeSync(fd, ' first data part '); // asynchronously write data to the file fs.write(fd, ' second data part ', function(err){ if(err) console.log( err ); }); // asynchronously close the file descriptor fs.closeSync(fd); 

The asynchronous write method successfully writes the data to the file, BUT NOT always! (method throws an error of 'bad file descriptor')

I expected an error by the asynchronous 'write()' method because the file descriptor is closed synchronously so it shouldn't have a valid file descriptor to work with (but it's not the case at least not always)

Here are questions

  • Does that mean that asynchronous tasks are executed separately by libuv running a separate JS thread so this causes the above example not to crash?

  • If I'm right above is this the same behavior in the browser (is there any asynchronous difference between browsers?)

  • If I'm wrong how the hell the above asynchronous 'write()' method gets the file descriptor?

2 Answers 2

1

Does that mean that asynchronous tasks are executed separately by libuv running a separate JS tread so this causes the above example not to crash

Not a JavaScript thread, no. Node.js runs I/O tasks on a separate, non-JavaScript thread or threads — but more importantly, it uses asynchronous I/O features of the OS, where possible, to avoid even tying that thread / those threads up unnecessarily. Those features let it queue and process I/O progress/completions on that/those other thread(s), and then queue calls to the async function's callback (your write callback, in this case) where appropriate on the main JavaScript thread's event loop.

So what you have in your code is a race between threads that are not coordinated, which explains why sometimes it works and other times it fails. Thread scheduling can seem non-deterministic because of all the factors involved at the OS level.

Sign up to request clarification or add additional context in comments.

Comments

1

JavaScript is single-threaded to a point. JS's concurrency style is what's called non-blocking. The code is executed on one thread, the event loop, so you can't block it or everything stops (this is why alerts stop you from interacting with the page) But under the hood, the browser has threads for things like AJAX requests, rendering, event triggers, and file operations. When they capture any new event, they push it on an event queue and then that event loop, in which your code is running gets triggered and it handles the request.

1 Comment

This is Node.js, not a browser.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.