notenoughneon / typed-promisify Goto Github PK
View Code? Open in Web Editor NEWTypescript promise helpers
License: MIT License
Typescript promise helpers
License: MIT License
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
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>;
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.
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);
});
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.