The reason for the below code is to get rid of callback hell
/pyramid of doom
. I don't fully understand i/o blocking
though yet.
'use strict';
var fs = require('fs');
var co = require('co');
co(function* () {
var fileName = 'readme.txt';
var str =
yield new Promise(function (resolve, reject) {
var result;
try {
result = fs.readFileSync(fileName, 'utf8');
} catch (err) {
reject(err);
}
resolve(result);
});
console.log('result readFileSync: ' + str);
});
All I'm expecting is a yes
or no
answer to be honest. Hope fully if no could someone give some details as I'm trying to learn properly about JavaScript sync/async and how to harness the power of Promises.
log(data); }); Using the synchronous way with readFileSync . If there is a problem, an error will be thrown and you won't get to the last line.
In fs. readFile() method, we can read a file in a non-blocking asynchronous way, but in fs. readFileSync() method, we can read files in a synchronous way, i.e. we are telling node. js to block other parallel process and do the current file reading process.
Often Promise. all() is thought of as running in parallel, but this isn't the case. Parallel means that you do many things at the same time on multiple threads. However, Javascript is single threaded with one call stack and one memory heap.
Return Value: It returns a Promise. The Promise is resolved with the contents of the file. If no encoding is specified (using options. encoding), the data is returned as a Buffer object.
No
If you want to wrap a file read operation, try to use the async versions of Node functions as much as possible. Using readFileSync
with a promise gives you no advantage over using readFileSync
on its own, because readFileSync
blocks the process until it is done reading, which readFile
does not.
A better solution would therefore be something like this:
'use strict';
var fs = require('fs');
var readFilePromise = function(file) {
return new Promise(function(ok, notOk) {
fs.readFile(file, function(err, data) {
if (err) {
notOk(err)
} else {
ok(data)
}
})
})
}
readFilePromise('/etc/passwd').then(function(data) {
// do something with the data...
})
The correct way is to use the Node's native util library and promisfy fs.readFile
:
const path = require('path');
const fs = require('fs');
const INDEX = path.join(__dirname, 'app', 'index.html');
const readFile = require('util').promisify(fs.readFile);
readFile(INDEX)
.then(e => console.log(e.toString()))
.catch(e => console.log('FOOBAR ' + e));
result:
one@dolphin:~/github/resume $ node toolchain/inject.js
FOOBAR Error: ENOENT: no such file or directory, open '/home/one/github/resume/toolchain/app/index.html'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With