在 TypeScript 的异步函数中返回承诺

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43881192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:43:37  来源:igfitidea点击:

Returning a promise in an async function in TypeScript

typescript

提问by dumbmatter

It's my understanding that these two functions will have the same behavior in JavaScript:

我的理解是这两个函数在 JavaScript 中具有相同的行为:

const whatever1 = (): Promise<number> => {
    return new Promise((resolve) => {
        resolve(4);
    });
};

const whatever2 = async (): Promise<number> => {
    return new Promise((resolve) => {
        resolve(4);
    });
};

But TypeScript seems to not like the second one, it says:

但是 TypeScript 似乎不喜欢第二个,它说:

Type '{}' is not assignable to type 'number'.

Is this a bug in TypeScript, or am I misunderstanding something about async functions?

这是 TypeScript 中的错误,还是我误解了异步函数?

回答by artem

It's complicated.

情况很复杂。

First of all, in this code

首先,在这段代码中

const p = new Promise((resolve) => {
    resolve(4);
});

the type of pis inferred as Promise<{}>. There is open issueabout this on typescript github, so arguably this is a bug, because obviously (for a human), pshould be Promise<number>.

的类型p推断为Promise<{}>。在打字稿 github 上有一个关于这个的公开问题,所以可以说这是一个错误,因为显然(对于人类),p应该是Promise<number>.

Then, Promise<{}>is compatible with Promise<number>, because basically the only property a promise has is thenmethod, and thenis compatible in these two promise types in accordance with typescript rules for function types compatibility. That's why there is no error in whatever1.

然后,Promise<{}>与 兼容Promise<number>,因为基本上promise 唯一的属性是thenmethod,并且then在这两种promise 类型中按照函数类型兼容性的打字稿规则兼容。这就是为什么没有错误的原因whatever1

But the purpose of asyncis to pretend that you are dealing with actual values, not promises, and then you get the error in whatever2because {}is obvioulsy not compatible with number.

但是 的目的async是假装您正在处理实际值,而不是承诺,然后您会收到错误,whatever2因为{}显然与number.

So the asyncbehavior is the same, but currently some workaround is necessary to make typescript compile it. You could simply provide explicit generic argument when creating a promise like this:

所以async行为是相同的,但目前需要一些解决方法来使打字稿编译它。在创建这样的承诺时,您可以简单地提供明确的泛型参数:

const whatever2 = async (): Promise<number> => {
    return new Promise<number>((resolve) => {
        resolve(4);
    });
};

回答by Remo H. Jansen

When you do new Promise((resolve)...the type inferred was Promise<{}>because you should have used new Promise<number>((resolve).

当您执行new Promise((resolve)...推断类型时,是Promise<{}>因为您应该使用new Promise<number>((resolve).

It is interesting that this issue was only highlighted when the asynckeyword was added. I would recommend reporting this issue to the TS team on GitHub.

有趣的是,此问题仅在async添加关键字时才突出显示。我建议在 GitHub 上向 TS 团队报告这个问题。

There are many ways you can get around this issue. All the following functions have the same behavior:

有很多方法可以解决这个问题。以下所有函数都具有相同的行为:

const whatever1 = () => {
    return new Promise<number>((resolve) => {
        resolve(4);
    });
};

const whatever2 = async () => {
    return new Promise<number>((resolve) => {
        resolve(4);
    });
};

const whatever3 = async () => {
    return await new Promise<number>((resolve) => {
        resolve(4);
    });
};

const whatever4 = async () => {
    return Promise.resolve(4);
};

const whatever5 = async () => {
    return await Promise.resolve(4);
};

const whatever6 = async () => Promise.resolve(4);

const whatever7 = async () => await Promise.resolve(4);

In your IDE you will be able to see that the inferred type for all these functions is () => Promise<number>.

在您的 IDE 中,您将能够看到所有这些函数的推断类型是() => Promise<number>.