Created
June 18, 2017 15:33
-
-
Save franher/0680a03ae151e8df278c92699f47b92c to your computer and use it in GitHub Desktop.
Custom fs.stat promisify as identity function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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