I am migrating a code base written in Node v6.10 to v10.15.3,
I wish to use the async / await style and util library to avoid the callback hell my code has,
I am able to convert the functions into the new async/await style and use the util library functionality as well.
Someone highly recomended me to chain the .bind function to the util.promisify() function,
I understand that the .bind() is used for variable/object scope.
But is it really necessary to do the .bind after promisifying using the util lib?
Following is my sample code -
let fs = require('fs');
let util = require('util');
let test = async () => {
let keyPath = 'someFile.txt';
//This works
const fsReadFile = util.promisify(fs.readFile);
//This also works
//const fsReadFile = util.promisify(fs.readFile).bind(this);
//This also works
//const fsReadFile = util.promisify(fs.readFile).bind(fs);
var fileContent = await fsReadFile(keyPath, 'utf8');
console.log(fileContent);
};
test();
I followed the following sites for the implementation -
1. util implementation -
https://medium.com/@suyashmohan/util-promisify-in-node-js-v8-d07ef4ea8c53
2. understand bind function -
a) https://blog.cloudboost.io/nodejs-bind-function-e5d33ea081f0
b) Use of the JavaScript 'bind' method
Unsure if there are the usecases -
1. https://github.com/nodejs/node/issues/13338
2. Function works, but fails when util.promisify() used?
util.promisify
doesn't have a binding argument. So if you need to bind the context, do this:
const util = require('util');
const run = util.promisify(thing.run).bind(thing);
Source
No, in this case it is not needed.
If you take a look at readFile implementation it does not make use of this
at any moment:
https://github.com/nodejs/node/blob/master/lib/fs.js#L283
This means that the function will work if it loses its original context (the fs module) or if it is binded, it just does not matter. That's why all the examples you posted work without problems.
However there are many libraries that depends on its this
context, hence they need binding.
If someone is suggesting you to bind everything he/she probably does not have a big understanding of how javascript works, and they just prefer to bind everything because it will not hurt much if it is not needed (just a small performance hit that is not noticeable in any case).
My advice is to investigate each case separately or use libraries that uses promises natively.
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