From ccc3445354135398b6eb1a04c7d27c13b833f2d5 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Mon, 12 Jul 2021 14:26:21 -0700 Subject: [PATCH] Fix return `undefined` --- src/index.ts | 2 +- test/test.ts | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8030c3c..9f5c83f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -147,7 +147,7 @@ namespace degenerator { const r = function (this: any, ...args: A): Promise { try { const p = fn.apply(this, args); - if (typeof p.then === 'function') { + if (typeof p?.then === 'function') { return p; } return Promise.resolve(p); diff --git a/test/test.ts b/test/test.ts index 2fb9b87..e42fa72 100644 --- a/test/test.ts +++ b/test/test.ts @@ -136,7 +136,7 @@ describe('degenerator()', () => { 'Expected a "function" to be returned for `foo`, but got "number"' ); }); - it('should be compile if branches', () => { + it('should compile if branches', () => { function ifA(): string { if (a()) { return 'foo'; @@ -173,5 +173,12 @@ describe('degenerator()', () => { } assert.equal(err.message,'process is not defined') }); + it('should allow to return synchronous undefined', () => { + function u() {} + const fn = compile(`${u}`, 'u', ['']); + return fn().then(val => { + assert.strictEqual(val, undefined); + }); + }); }); });