From 3aa6caa70ef87047f52c786c097f2c9f8ab1f2ad Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 18 Oct 2019 15:32:14 -0700 Subject: [PATCH] Fix passing arguments to converted generator function --- src/generator-to-promise.ts | 8 ++++---- test/test.ts | 31 ++++++++++++++----------------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/generator-to-promise.ts b/src/generator-to-promise.ts index 500e8bb..249f56c 100644 --- a/src/generator-to-promise.ts +++ b/src/generator-to-promise.ts @@ -33,18 +33,18 @@ export default function generatorToPromise( ): (...args: any[]) => Promise { if (!isGen(generatorFunction)) { if (typeof generatorFunction === 'function') { - return function(this: any) { + return function(this: any, ...args: any[]) { return Promise.resolve(true).then(() => - generatorFunction.call(this, arguments) + generatorFunction.apply(this, args) ); }; } throw new Error('The given function must be a generator function'); } - return function(this: any) { + return function(this: any, ...args: any[]) { const deferred = createDeferred(); - const generator = generatorFunction.call(this, arguments); + const generator = generatorFunction.apply(this, args); (function next(error?: Error | null, value?: any) { let genState = null; try { diff --git a/test/test.ts b/test/test.ts index 96c9f90..c871b44 100644 --- a/test/test.ts +++ b/test/test.ts @@ -95,13 +95,13 @@ describe('degenerator()', () => { }); describe('`compile()`', () => { - it('should compile code into an invocable async function', done => { - const a = () => Promise.resolve('a'); + it('should compile code into an invocable async function', () => { + const a = (v: string) => Promise.resolve(v); const b = () => 'b'; - function aPlusB(): string { - return a() + b(); + function aPlusB(v: string): string { + return a(v) + b(); } - const fn = compile<() => Promise>( + const fn = compile<(v: string) => Promise>( '' + aPlusB, 'aPlusB', ['a'], @@ -109,12 +109,11 @@ describe('degenerator()', () => { sandbox: { a, b } } ); - fn().then((val: string) => { - assert.equal(val, 'ab'); - done(); - }, done); + return fn('c').then((val: string) => { + assert.equal(val, 'cb'); + }); }); - it('should be able to await non-promises', done => { + it('should be able to await non-promises', () => { const a = () => 'a'; const b = () => 'b'; function aPlusB(): string { @@ -128,12 +127,11 @@ describe('degenerator()', () => { sandbox: { a, b } } ); - fn().then((val: string) => { + return fn().then((val: string) => { assert.equal(val, 'ab'); - done(); - }, done); + }); }); - it('should be able to compile functions with no async', done => { + it('should be able to compile functions with no async', () => { const a = () => 'a'; const b = () => 'b'; function aPlusB(): string { @@ -147,10 +145,9 @@ describe('degenerator()', () => { sandbox: { a, b } } ); - fn().then((val: string) => { + return fn().then((val: string) => { assert.equal(val, 'ab'); - done(); - }, done); + }); }); it('should throw an Error if no function is returned from the `vm`', () => { let err;