Created
August 21, 2024 13:17
-
-
Save DIY0R/a0c714e35d8f842aaaa973be0c52ed29 to your computer and use it in GitHub Desktop.
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 promisify = (fn) => (...args) => { | |
const promise = new Promise((resolve, reject) => { | |
const callback = (err, data) => { | |
if (err) reject(err); | |
else resolve(data); | |
}; | |
fn(...args, callback); | |
}); | |
return promise; | |
}; | |
// Usage | |
const fs = require('node:fs'); | |
const read = promisify(fs.readFile); | |
const main = async () => { | |
const fileName = '1-promisify.js'; | |
const data = await read(fileName, 'utf8'); | |
console.log(`File "${fileName}" size: ${data.length}`); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment