Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include async functions in origins promise detection #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

oshoemaker
Copy link

Current promise detection does not properly detect async functions in ES2018.

@fengmk2
Copy link
Member

fengmk2 commented Dec 21, 2019

@oshoemaker can you add a test for this bug fix?

@oshoemaker
Copy link
Author

@oshoemaker can you add a test for this bug fix?

@fengmk2, I am not really sure how this can be tested. In our case we are using serverless web pack with target ES2018 and ESNext lib. The issue is that the test would succeed/fail based on node version/transpiler settings. So I can incorporate a test but would need to change the test environment in order to actually test this.

Suggestions?

@oshoemaker oshoemaker force-pushed the os/bug/fix-async-detection branch 3 times, most recently from 340a8eb to 56ab6cf Compare December 26, 2019 19:17
@oshoemaker
Copy link
Author

I have added a "test" to force it to use the code and resulting output. I am not really sure if there is any other option with the current test configuration. Happy to hear suggestions though.

@oshoemaker
Copy link
Author

@fengmk2, any update on this?

Copy link
Member

@3imed-jaberi 3imed-jaberi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀 ..

@@ -56,7 +56,8 @@ module.exports = function(options) {
let origin;
if (typeof options.origin === 'function') {
origin = options.origin(ctx);
if (origin instanceof Promise) origin = await origin;
const isAsync = origin.constructor.name === 'AsyncFunction';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why we need this detect? When we call an async function, it will return a promise instance.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dead-horse , not always, the async function return a promise instance.

const p = new Promise(()=>{});
p instanceof Promise // true
const  a = async function () {}
a instanceof Promise // false
a.constructor.name === "AsyncFunction" // true 

chrome console
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const  a = async function () {}
const result = a()
result instanceof Promise // true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants