Skip to content

Instantly share code, notes, and snippets.

@franher
Created June 18, 2017 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save franher/0680a03ae151e8df278c92699f47b92c to your computer and use it in GitHub Desktop.
Save franher/0680a03ae151e8df278c92699f47b92c to your computer and use it in GitHub Desktop.
Custom fs.stat promisify as identity function
const util = require('util');
const fs = require('fs');
/**
* identity function f(v) -> v
* Using ES6 Promise
*
* async/await approach:
* async function identityAsync(v) {
* return v;
* }
*/
function identityAsync(v) {
return new Promise((resolve, reject) => resolve(v));
}
// Overriding promisify fs.stat funtion as identityAsync
fs.stat[util.promisify.custom] = identityAsync;
const customStatPromisified = util.promisify(fs.stat);
customStatPromisified('../path')
.then(stats => {
// Should equal to '../path'
console.error(stats);
})
.catch(error => {
// Handle the error.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment