Code Monkey home page Code Monkey logo

typed-promisify's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

typed-promisify's Issues

Should ship type definition file instead of source files

Currently the source file (.ts) is in the npm package and there is no type definition file (.d.ts) included in the package. This causes an issue (see https://github.com/Microsoft/TypeScript/wiki/FAQ#why-is-a-file-in-the-exclude-list-still-picked-up-by-the-compiler) where the typed-promisify code is always picked up by tsc in my project and included in my module, instead of installed later as a dependency.

The solution is to have tsc emit a type definition file at build and to exclude the source files from the npm publish command

Nullable callback cause error

Some library declare a function like

get(sql: string, params: any, callback?: (err: Error, row: any) => void): Database;

This would make an error when tp library declare callback function couldn't be null

export function promisify<A, A2, T>(f: (arg: A, arg2: A2, cb: (err: any, res: T) => void) => void, thisContext?: any): (arg: A, arg2: A2) => Promise<T>;

Build errors when noImplicitAny enabled

node_modules/typed-promisify/index.ts(7,27): error TS7006: Parameter 'f' implicitly has an 'any' type.
node_modules/typed-promisify/index.ts(7,30): error TS7006: Parameter 'thisContext' implicitly has an 'any' type.
node_modules/typed-promisify/index.ts(11,24): error TS7006: Parameter 'err' implicitly has an 'any' type.
node_modules/typed-promisify/index.ts(11,29): error TS7006: Parameter 'result' implicitly has an 'any' type.
node_modules/typed-promisify/index.ts(17,68): error TS7006: Parameter 'T' implicitly has an 'any' type.
node_modules/typed-promisify/index.ts(18,55): error TS7006: Parameter 'T' implicitly has an 'any' type.
node_modules/typed-promisify/index.ts(19,55): error TS7006: Parameter 'T' implicitly has an 'any' type.
node_modules/typed-promisify/index.ts(20,42): error TS7006: Parameter 'T' implicitly has an 'any' type.
node_modules/typed-promisify/index.ts(22,21): error TS7006: Parameter 'elts' implicitly has an 'any' type.
node_modules/typed-promisify/index.ts(22,27): error TS7006: Parameter 'f' implicitly has an 'any' type.
node_modules/typed-promisify/index.ts(23,17): error TS7006: Parameter 'elts' implicitly has an 'any' type.
node_modules/typed-promisify/index.ts(23,46): error TS7006: Parameter 'elt' implicitly has an 'any' type.
node_modules/typed-promisify/index.ts(33,22): error TS7006: Parameter 'f' implicitly has an 'any' type.
node_modules/typed-promisify/index.ts(33,25): error TS7006: Parameter 'thisContext' implicitly has an 'any' type.

Add Optional thisContext and fix error for new typescript 2.1.5

Thanks for the implementation!

Here is a quick improvement I needed that allows preserving the this context when needed:

export type Nodeback<T> = (err: any, result: T) => void;

export function promisify<T>(f: (cb: Nodeback<T>) => void, thisContext: any): () => Promise<T>;
export function promisify<A, T>(f: (arg: A, cb: Nodeback<T>) => void, thisContext: any): (arg: A) => Promise<T>;
export function promisify<A, A2, T>(f: (arg: A, arg2: A2, cb: Nodeback<T>) => void, thisContext: any): (arg: A, arg2: A2) => Promise<T>;
export function promisify<A, A2, A3, T>(f: (arg: A, arg2: A2, arg3: A3, cb: Nodeback<T>) => void, thisContext: any): (arg: A, arg2: A2, arg3: A3) => Promise<T>;
export function promisify<A, A2, A3, A4, T>(f: (arg: A, arg2: A2, arg3: A3, arg4: A4, cb: Nodeback<T>) => void, thisContext: any): (arg: A, arg2: A2, arg3: A3, arg4: A4) => Promise<T>;
export function promisify(f: any, thisContext: any = null) {
    return function () {
        let args = Array.prototype.slice.call(arguments);
        return new Promise((resolve, reject) => {
            args.push((err: any, result: any) => err !== null ? reject(err) : resolve(result));
            f.apply(thisContext, args);
        });
    }
}

Oh, I also fixed the error by moving the args up:

    return function () {
        // To Here:
        let args = Array.prototype.slice.call(arguments);
        return new Promise((resolve, reject) => {
            // From Here
            args.push((err: any, result: any) => err !== null ? reject(err) : resolve(result));
            f.apply(thisContext, args);
        });
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.