Skip to content
This repository has been archived by the owner on May 5, 2023. It is now read-only.

Commit

Permalink
Fix passing arguments to converted generator function
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Oct 18, 2019
1 parent ee8b244 commit 3aa6caa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/generator-to-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ export default function generatorToPromise<T>(
): (...args: any[]) => Promise<T> {
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<T>();
const generator = generatorFunction.call(this, arguments);
const generator = generatorFunction.apply(this, args);
(function next(error?: Error | null, value?: any) {
let genState = null;
try {
Expand Down
31 changes: 14 additions & 17 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,26 +95,25 @@ 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<string>>(
const fn = compile<(v: string) => Promise<string>>(
'' + aPlusB,
'aPlusB',
['a'],
{
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 {
Expand All @@ -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 {
Expand All @@ -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;
Expand Down

0 comments on commit 3aa6caa

Please sign in to comment.