-
Notifications
You must be signed in to change notification settings - Fork 26
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
Add async flag to methods and functions #33
Comments
Sorry, to be clear I actually think that we shouldn't add a special flag for async functions. There can be very minor differences, but a function that returns a Promise and doesn't early return or throw is indistinguishable from an async function. I don't think the labelling of a function should change if the implementation does, but it's external contract doesn't. |
I think it kind of makes sense to support the I can definitely see a need from developers to be able to easily document whether or not a function is async, and it would be a very small change/addition to the schema |
The IMO, |
This is only a problem for implementers of analyzers, right? It would suck to have documentation viewers/tools have to specifically check whether or not the Given the following example, using plain JS: async function foo() {
return /* whatever, really */;
} I would still like to be able to document this function as being async, because that completely changes how you use it. We have a native |
For the sake of completeness: function a(f) {
if (typeof f !== 'function')
throw new Error('no func');
else
return Promise.resolve(f());
} async function b(f) {
if (typeof f !== 'function')
throw new Error('no func');
else
return await f();
} assert(a(null) instanceof Promise); // throws assert(b(null) instanceof Promise); // true
assert(b(() => 0) instanceof Promise); // true These two functions have meaningfully and observably different behaviours, even if we'd replace the return statement in |
Circling back to this, I still don't fully agree that async functions are materially different from functions that return Promises. Would this be marked as an async function? function foo() {
return new Promise((res) => res(something));
} Or bar here: async function foo(x) { ... }
function bar() { return foo(0); } They have exactly the same qualities as an async function, including not returning or throwing synchronously. I would say that because the behavior is the same and the difference unobservable, then it should be marked async. But then, how would that be done? Should a tool see that a function returns a Promise - and mark all Promise-returning functions as async? Maybe, but that tool should also write that the return type is Promise. At that point there's really almost no difference between async functions and Promise-returning functions in the manifest. And most functions should not return a Promise or non-Promise, but if they do that should be documented and visible in the return type, not with |
but what about function() {
if (cond)
throw new Error('sync')
else
return new Promise((_, rej) => rej('async'))
} async functions are different to regular functions, even if in many cases they behave the same |
While subsequent tooling could infer this from the return type, @justinfagnani rightly pointed out that there are some semantic differences, notably for error types, and perhaps for early returns too.
API tables would benefit from this flag as well:
The text was updated successfully, but these errors were encountered: