Skip to content

Instantly share code, notes, and snippets.

@ste2425
Created November 2, 2016 16:08
Show Gist options
  • Save ste2425/a284ead2ac823e61cd2a07ac237b7176 to your computer and use it in GitHub Desktop.
Save ste2425/a284ead2ac823e61cd2a07ac237b7176 to your computer and use it in GitHub Desktop.
Little promisify helper
let promisify = (fn) => (...args) =>
new Promise((rCB, eCB) =>
fn(...args, (e, r) => {
if (e)
eCB(e);
else
rCB(r);
}));
promisify(sass.render({
file: 'myFile.scss'
))
.then(r => promisify(mkdir)(destPathFolder).then(x => r.css))
.then(css => promisify(fs.writeFile)(destPath, css))
.then(d => console.log('All Done!'))
.catch(e => console.error(`Proccess error: ${e}`));
@ste2425
Copy link
Author

ste2425 commented Nov 3, 2016

Found a flaw, if you provide the call back when calling the promisified function it will still return a promise. However the promise will never resolve, instead the callback will run like it wasn't promisified in the first place.

var test = promisify(fs.writeFile);

var p = test('dest.css', 'some css', (e, r) => {
  // I'm called
});

// p is a promise here
p.then(r => {
   // never called
});

I guess you could check the last argument passed in to see if its a function and make assumptions based on that but that seems painful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment