Hello,
I'm wondering how to write sync code in Node.js like for e.g. file.copySync
which copy files synchronously. I know it's bad practice to write sync code in Node.js, because it lock up server, but I'm just wondering how. It will be awesome if someone can write some really light code to show it or maybe have a link to any docs?
Thanks!
It is not always a bad practice. You can safely use sync code in CLI applications, for example. That is why all Node.js file system functions have their sync counterparts with Sync
suffix (see fs
doc).
Currently, there is no lib function to copy a file, but you can use any system utility with Node.js child_process.execSync()
method.
For example, this code synchronously copies a file on Windows:
const childProcess = require('child_process');
childProcess.execSync('copy test.js test2.js');
console.log('Done!');
If you have more questions, feel free to ask in Node.js help repository.
Such questions are better asked in the help repo, the Gitter channel and #node.js on IRC.
~Since the question has been answered, I'm going to go ahead and close it. Thanks!~ Oops, became closed right when I was about to click "Close and comment".
Feel free to reopen it and turn into documentation issue if you believe that docs on this topic are not sufficient, confusing or anything.
@vsemozhetbyt, @aqrln The last question, everything I write in Node.js is sync? For e.g. sync stuff:
function a() {
let a = 0;
for (i = 0; i < 100000000; i++) {
a++;
}
b();
}
function b() {
let a = 0;
for (i = 0; i < 100000000; i++) {
a++;
}
c();
}
function c() {
for (i = 0; i < 100000000; i++) {
}
console.log('sync finished!');
}
a();
console.log('This should be good');
async stuff:
function a() {
setTimeout(
() => {
let a = 0;
for (i = 0; i < 100000000; i++) {
a++;
}
b();
},
0
);
}
function b() {
setTimeout(
() => {
let a = 0;
for (i = 0; i < 100000000; i++) {
a++;
}
c();
},
0
);
}
function c() {
setTimeout(
() => {
for (i = 0; i < 100000000; i++) {
}
console.log('async finished!');
},
0
);
}
a();
console.log('This should be good');
I'm just confused, but after these sync and async functions calls I think I get the thing, I just need the last answer if I get it right or no (I mean we almost always write sync code which can be turned to async by Promises, async/await and so on, I'm not talking about dependencies which have async methods and as far as I know almost all Node.js dependencies are async by default)?
I am not sure if I understand the question rightly. If nobody else answers, please, could you elaborate?
By the way, you can clarify this topic with these guides:
https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/
https://nodejs.org/en/docs/guides/blocking-vs-non-blocking/
@vsemozhetbyt Thank you for guides. I get the thing about sync/async after reading guides and also testing some code. I just misunderstood some things about Node.js.
@svipben Well, it seems this is really a rather confusing thing in Node.js. So fill free to ask any questions in the future)
In your example, the loops are all sync, they just get started by some async code. All JavaScript is sync, it's only the parts that defer to the native side that are actually async. Basically, if a method in the Node.js API has a *Sync()
version, the method without the Sync
suffix will store the callback to run when the event loop in the native side sees the corresponding async task complete. When that task completes, a task to run the callback will be queued and the JavaScript thread should eventually run that callback. Things like the very long loops above can potentially block the JavaScript thread for a long period of time, preventing those callbacks from running and slowing down your app.
Does that make sense? It's a bit difficult to wrap your head around at first but basically just try to keep a mental note that everything is sync unless it has a callback.
(Although, things with callbacks are not necessarily async. Array functions like forEach, map, filter, etc are sync. Most things with callbacks are async though.)
@Qard Thank you very much. Right now everything seems more clear than ever before.
Most helpful comment
It is not always a bad practice. You can safely use sync code in CLI applications, for example. That is why all Node.js file system functions have their sync counterparts with
Sync
suffix (seefs
doc).Currently, there is no lib function to copy a file, but you can use any system utility with Node.js
child_process.execSync()
method.