npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

promisify-core-api

v1.0.1

Published

A Polyfill for Promisify Node ^8.0.0 Core Api

Downloads

3

Readme

Promisify Core Api

this is a node module who will inject new Async methods im core module of NodeJS based on Sync function with usage of callback method promisify by util.promisify(callback)

For example, in NodeJS we have in fs :

  • fs.readFileSync(path) : string (bad idea)
  • fs.readFile(path, callback: (err, content :str) => void) : void (good but risk of callback hell)

With require this module you'll have a new function :

  • fs.readFileAsync(path) : Promise<content: string, err> (Yeah a Promise, we can chain, await, etc..., help to keep a flat code ^^)

Requirements

no-one except Node 8+

The big magic is in the new util.promisify function added in Node 8.

Install

npm install promisify-core-api --save

Usage

In the earlier entry point of your program :

require('promisify-core-api');

And thats it, now, everywhere on your program, you can replace a Sync Node Core function by Async and use use the Promise returned.

Example

With Promise style

require('promisify-core-api'); // at the very earlier entry point of your program, One time

// somewhere in your code
const path = require('path');
const fs = require('fs');

// then
fs.readFileAsync(path.join(__filename))
  .then(txt => console.log(txt.toString()))
  .catch(err => console.log(err));

// catch
fs.readFileAsync(path.join(__dirname, __dirname))
  .then(txt => console.log(txt.toString()))
  .catch(err => console.log(err));

With async - await style

require('promisify-core-api'); // at the very earlier entry point of your program, One time

// somewhere in your code
const path = require('path');
const fs = require('fs');

// async IIFE, yeah it's preaty ugly but we can't await in global scope, global scope is not async
(async () => {
    // then
    try {
        const txt = await fs.readFileAsync(path.join(__filename));
        console.log(txt.toString());
    } catch (err) {
        console.log(err);
    }

    // catch
    try {
        const txt = await fs.readFileAsync(path.join(__dirname, __dirname));
        console.log(txt.toString());
    } catch (err) {
        console.log(err);
    }
})();

Bonus

const functions = require('promisify-core-api');

console.log(functions);

You will see all supported functions, and get original sync, callback api and the promisify api (function name, not directly the function).

See below part of this big object

{
    'require(\'fs\')': [
        {
            sync: 'accessSync',
            promise: 'accessAsync',
            callback: 'access'
        },
        {
            sync: 'existsSync',
            promise: 'existsAsync',
            callback: 'exists'
        },
        {
            sync: 'readFileSync',
            promise: 'readFileAsync',
            callback: 'readFile'
        },
        {
            sync: 'closeSync',
            promise: 'closeAsync',
            callback: 'close'
        },
        {
            sync: 'openSync',
            promise: 'openAsync',
            callback: 'open'
        },
        {
            sync: 'readSync',
            promise: 'readAsync',
            callback: 'read'
        },
        {
            sync: 'writeSync',
            promise: 'writeAsync', 
            callback: 'write'
        },
        {
            sync: 'renameSync',
            promise: 'renameAsync',
            callback: 'rename'
        }, ...
    ], ...
}