Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

Use the node v8 promisify API to remove unnecessary code #153

Closed
addityasingh opened this issue Jun 5, 2017 · 4 comments · Fixed by #174
Closed

Use the node v8 promisify API to remove unnecessary code #153

addityasingh opened this issue Jun 5, 2017 · 4 comments · Fixed by #174

Comments

@addityasingh
Copy link
Contributor

addityasingh commented Jun 5, 2017

We can remove some unnecessary explicit promisify code by using the v8 utils.promisify API (https://nodejs.org/en/blog/release/v8.0.0/#improved-support-for-promises).

E.g.: This function can be changed to a much elegant and declarative implementation.

const readFile = (path) => 
    new Promise((resolve, reject) => {
        fs.readFile(path, 'utf-8', (err, data) => {
            if (err) {
                reject(new TemplateError(err));
                return;
            } 
            resolve(data);
        });
    });

to

const util = require('util');

const readFile = (path) => 
    util.promisify(fs.readFile)(path)
        .catch(err => new TemplateError(err));
@vigneshshanmugam
Copy link
Collaborator

Yup nice idea, but we need to polyfill it in lower versions. https://github.com/ljharb/util.promisify

@addityasingh
Copy link
Contributor Author

Agreed. I will use this to have a polyfill for node 6.0 - node < v8.0

@dazld
Copy link
Contributor

dazld commented Jun 12, 2017

should promisfy once and use the returned function instead of doing it on every invocation.

@dazld
Copy link
Contributor

dazld commented Jun 12, 2017

ie:

const readFileP = util.promisify(fs.readFile); // or whatever

const readFile = (path) => readFileP(path).catch(err => new TemplateError(err));

vigneshshanmugam pushed a commit that referenced this issue Sep 4, 2017
* #172 Use the node v8 promisify API to remove unnecessary code

* #172 Remove unnecessary space

* #172 #153 Add yarn lock file
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
3 participants