Skip to content

Instantly share code, notes, and snippets.

@DIY0R
Created August 21, 2024 13:17
Show Gist options
  • Save DIY0R/a0c714e35d8f842aaaa973be0c52ed29 to your computer and use it in GitHub Desktop.
Save DIY0R/a0c714e35d8f842aaaa973be0c52ed29 to your computer and use it in GitHub Desktop.
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