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?