-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Support async function type #14027
Comments
I think that would be misleading because it would suggest that returning a |
@jesseschalken TS implements ES2016 async/away proposal which explicitly states, that the return value is a Promise. So the implication of the return value is valid, unless I'm missing something ... Could you provide an example where this wouldn't be true ? |
I'm not saying |
@jesseschalken an |
@yortus When I say "is If you add the |
@jesseschalken OK I get you now. But still, there's no observable distinction between the two ways of writing the same thing. Isn't it a bit like saying |
I believe that from the caller perspective (and from one who implements an interface) it makes sense to know which methods are supposed to be async and can be It aligns nicely with checking the resolved value of promises.
I believe the opposite is true. "Async" describes a pattern, while |
Imagine, hypothetically, at some point in the future |
I've updated this issue's description to point out, that the scope is having a way to "specify an async method" as opposed to making it equivalent to Promise return value. |
@Thinkscape returning Also, The only justification I can see for the syntax you propose is convenience. It doesn't introduce any functional distinctions IMO. It also brings back up the same issue of potentially misleading return type annotation that was discussed in #7284. |
A caller cannot tell the difference between
You can use
A |
IMHO The discussion is on a sidetrack... whether or not there is a difference to a client, it would be nice if an async function could be declared as having the type of an async function. In fact this is more important if async functions are different than functions returning promises. BTW there is discernable difference between a function an an async function, at least in node:
|
They don't make a difference to the type system: function foo() { };
async function bar() { };
(async () => {
const baz = await foo();
const qat = await bar();
})(); Be they |
Hmm... if I have a javascript function:
How should I notate it in typescript? It would seem best would be:
Then the return type of the overall function can be You can do more to a function besides calling it. Async functions and functions should be different types because they have different constructors. They can inherit from a common base type of course.... |
@shaunc Couple tips:
They have different constructors, but they are both callable, and TS only cares about that much. In particular, it doesn't even type generator functions as anything beyond TypeScript is a structurally typed language, not nominally, so leverage that to your advantage. Also, it's bad practice in general to check types much narrower than what you actually need, this being no exception. |
It's not obvious to me that For example, I was playing around with adding Go's
But using
Even though using
Ideally Obviously this isolated use-case isn't enough to warrant a change to the language, but I think it's suggestive that preventing programs from knowing whether something is an |
@verticalpalette I've personally found it's almost never actually useful to draw a distinction between the two when you're not actually needing to parse the function. And when you actually do need to draw the distinction without parsing, it's almost always for pretty-printing, etc., not actually matching the function or anything. Oh, and also, your // Original:
const myAsyncFunc = deferring(async defer => {
const resource = openResource();
defer(() => resource.close());
// readAsync throws an error because the resource is closed immediately
const contents = await resource.readAsync();
return performComputation(contents);
});
const myPromiseFunc = deferring(defer => {
const resource = openResource();
defer(() => resource.close());
// much more obvious that readAsync will throw an error
return resource.readAsync().then(performComputation);
});
// Transformed:
const myAsyncFunc = async () => {
const resource = openResource();
try {
// readAsync throws an error because the resource is closed immediately
const contents = await resource.readAsync();
return performComputation(contents);
} finally {
resource.close()
}
};
const myPromiseFunc = () => {
const resource = openResource();
const close$ = () => resource.close();
let returning$ = false;
try {
const p$ = run();
if (returning$ = p$ == null || typeof p$.then !== "function") return p$;
return new Promise((resolve, reject) => {
p$.then(
x => { try { close(); resolve(x) } catch (e) { reject(e) } },
e => { try { close(); reject(e) } catch (e) { reject(e) } }
);
});
} finally {
if (returning$) close$();
}
}; And honestly, it's rare for |
TypeScript Version: 2.1.1 / nightly (2.2.0-dev.201xxxxx)
Can we support async function type? That would make a lot of async code much cleaner.
For example:
Code
Expected behavior:
Because TS currently implements ES2016 async/await proposal, under the hood it'd be equivalent to:
Note: using
Promise
is just an implementation detail and could change in the future. What would stay the same is the fact, that the method returns its values async and can be "awaited".Actual behavior:
TS error:
The text was updated successfully, but these errors were encountered: