Skip to content

Instantly share code, notes, and snippets.

@jeswin
Last active December 23, 2015 03:08
Show Gist options
  • Save jeswin/17df57071349c92639d2 to your computer and use it in GitHub Desktop.
Save jeswin/17df57071349c92639d2 to your computer and use it in GitHub Desktop.
/* Created for discussion */
//interfaces/mongodb.js
declare module "mongodb" {
declare class Collection {
insertOne(doc: Object, cb: (err?: Error, result: { insertedId: string }) => void) : void;
}
}
//interfaces/nodefunc-promisify.js
declare module "nodefunc-promisify" {
declare type AsyncFunc<TResult> = () => Promise<TResult>;
declare type AsyncFunc1<T1, TResult> = (p1: T1) => Promise<TResult>;
declare type AsyncFunc2<T1, T2, TResult> = (p1: T1, p2: T2) => Promise<TResult>;
//...
declare function exports<T1, TResult>(
nodefunc: (p1: T1, cb: NodeCallbackType<TResult>) => void
) : AsyncFunc1<T1, TResult>;
//...
}
//app.js
// : Part 1: This works.
const _insertOne: AsyncFunc1<Object, { insertedId: string }> = promisify(MongoDb.Collection.prototype.insertOne);
const { insertedId } = await _insertOne.call(this.underlying); //error "too few arguments"
const { insertedId } = await _insertOne.call(this.underlying, { a: 1 }); //OK!
// : Part 2: This does NOT seem to work.
const _insertOne = promisify(MongoDb.Collection.prototype.insertOne);
const { insertedId } = await _insertOne.call(this.underlying); //OK!? But should not be OK.
/* Dumped types*/
25:20-28: ((nodefunc: (cb: (err?: Error) => void) => void) => () => Promise) & <T1> (nodefunc: (p1: T1, cb: (err?: Error) => void) => void) => (<T1> [type: (p1: T1) => Promise]) <T1> & <T1, T2> (nodefunc: (p1: T1, p2: T2, cb: (err?: Error) => void) => void) => (<T1, T2> [type: (p1: T1, p2: T2) => Promise]) <T1, T2> & <T1, T2, T3> (nodefunc: (p1: T1, p2: T2, p3: T3, cb: (err?: Error) => void) => void) => (<T1, T2, T3> [type: (p1: T1, p2: T2, p3: T3) => Promise]) <T1, T2, T3> & <T1, T2, T3, T4> (nodefunc: (p1: T1, p2: T2, p3: T3, p4: T4, cb: (err?: Error) => void) => void) => (<T1, T2, T3, T4> [type: (p1: T1, p2: T2, p3: T3, p4: T4) => Promise]) <T1, T2, T3, T4> & <T1, T2, T3, T4, T5> (nodefunc: (p1: T1, p2: T2, p3: T3, p4: T4, p5: T5, cb: (err?: Error) => void) => void) => (<T1, T2, T3, T4, T5> [type: (p1: T1, p2: T2, p3: T3, p4: T4, p5: T5) => Promise]) <T1, T2, T3, T4, T5> & <T1, T2, T3, T4, T5, T6> (nodefunc: (p1: T1, p2: T2, p3: T3, p4: T4, p5: T5, p6: T6, cb: (err?: Error) => void) => void) => (<T1, T2, T3, T4, T5, T6> [type: (p1: T1, p2: T2, p3: T3, p4: T4, p5: T5, p6: T6) => Promise]) <T1, T2, T3, T4, T5, T6> & <T1, TResult> (nodefunc: (p1: T1, cb: (<TResult> [type: (err?: Error, result: TResult) => void]) <TResult>) => void) => (<T1, TResult> [type: (p1: T1) => (<R> [class: Promise]) <TResult>]) <T1, TResult> & <T1, T2, TResult> (nodefunc: (p1: T1, p2: T2, cb: (<TResult> [type: (err?: Error, result: TResult) => void]) <TResult>) => void) => (<T1, T2, TResult> [type: (p1: T1, p2: T2) => (<R> [class: Promise]) <TResult>]) <T1, T2, TResult> & <T1, T2, T3, TResult> (nodefunc: (p1: T1, p2: T2, p3: T3, cb: (<TResult> [type: (err?: Error, result: TResult) => void]) <TResult>) => void) => (<T1, T2, T3, TResult> [type: (p1: T1, p2: T2, p3: T3) => (<R> [class: Promise]) <TResult>]) <T1, T2, T3, TResult> & <T1, T2, T3, T4, TResult> (nodefunc: (p1: T1, p2: T2, p3: T3, p4: T4, cb: (<TResult> [type: (err?: Error, result: TResult) => void]) <TResult>) => void) => (<T1, T2, T3, T4, TResult> [type: (p1: T1, p2: T2, p3: T3, p4: T4) => (<R> [class: Promise]) <TResult>]) <T1, T2, T3, T4, TResult> & <T1, T2, T3, T4, T5, TResult> (nodefunc: (p1: T1, p2: T2, p3: T3, p4: T4, p5: T5, cb: (<TResult> [type: (err?: Error, result: TResult) => void]) <TResult>) => void) => (<T1, T2, T3, T4, T5, TResult> [type: (p1: T1, p2: T2, p3: T3, p4: T4, p5: T5) => (<R> [class: Promise]) <TResult>]) <T1, T2, T3, T4, T5, TResult> & <T1, T2, T3, T4, T5, T6, TResult> (nodefunc: (p1: T1, p2: T2, p3: T3, p4: T4, p5: T5, p6: T6, cb: (<TResult> [type: (err?: Error, result: TResult) => void]) <TResult>) => void) => (<T1, T2, T3, T4, T5, T6, TResult> [type: (p1: T1, p2: T2, p3: T3, p4: T4, p5: T5, p6: T6) => (<R> [class: Promise]) <TResult>]) <T1, T2, T3, T4, T5, T6, TResult> & <TResult> (nodefunc: (cb: (err?: Error, result: any) => void) => void) => (<TResult> [type: () => (<R> [class: Promise]) <TResult>]) <TResult>
25:20-68: (<T1, T2, T3> [type: (p1: T1, p2: T2, p3: T3) => Promise]) <void, void, void> | (<T1, T2> [type: (p1: T1, p2: T2) => Promise]) <void, void> | (<T1> [type: (p1: T1) => Promise]) <void>
25:30-36: {Collection: [class: Collection], Cursor: [class: Cursor], Db: [class: Db], MongoClient: [class: MongoClient],}
25:30-47: [class: Collection]
25:30-57: Collection
25:30-67: (doc: Object, cb: (<TResult> [type: (err?: Error, result: TResult) => void]) <{insertedId: string,}>) => void
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment