本文介绍了TypeScript中的等待类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaScript中使用async/等待很多.现在,我逐渐将代码库的某些部分转换为TypeScript.

I use async / await a lot in JavaScript. Now I’m gradually converting some parts of my code bases to TypeScript.

在某些情况下,我的函数接受将被调用并等待的函数.这意味着它可以返回一个promise,而只是一个同步值.我为此定义了 Awaitable 类型.

In some cases my functions accept a function that will be called and awaited. This means it may either return a promise, just a synchronous value. I have defined the Awaitable type for this.

type Awaitable<T> = T | Promise<T>;

async function increment(getNumber: () => Awaitable<number>): Promise<number> {
  const num = await getNumber();
  return num + 1;
}

可以这样称呼:

// logs 43
increment(() => 42).then(result => {console.log(result)})

// logs 43
increment(() => Promise.resolve(42)).then(result => {console.log(result)})

这有效.但是,必须为我所有使用async/await和TypeScript的项目指定 Awaitable ,这很烦人.

This works. However, it is annoying having to specify Awaitable for all of my projects that use async/await and TypeScript.

我真的不能相信没有内置这种类型,但是我找不到.TypeScript是否具有内置的等待类型?

I can’t really believe such a type isn’t built in, but I couldn’t find one. Does TypeScript have a builtin awaitable type?

推荐答案

我相信这个问题的答案是:不,没有内置类型.

I believe the answer to this question is: No, there is no built-in type for this.

lib.es5.d.ts lib.es2015.promise.d.ts ,它们使用 T |PromiseLike< T> 对于您的 Awaitable< T> 有意义的各个地方,例如:

In lib.es5.d.ts and lib.es2015.promise.d.ts, they use T | PromiseLike<T> for the various places your Awaitable<T> would make sense, for instance:

/**
 * Represents the completion of an asynchronous operation
 */
interface Promise<T> {
    /**
     * Attaches callbacks for the resolution and/or rejection of the Promise.
     * @param onfulfilled The callback to execute when the Promise is resolved.
     * @param onrejected The callback to execute when the Promise is rejected.
     * @returns A Promise for the completion of which ever callback is executed.
     */
    then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;

    /**
     * Attaches a callback for only the rejection of the Promise.
     * @param onrejected The callback to execute when the Promise is rejected.
     * @returns A Promise for the completion of the callback.
     */
    catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T | TResult>;
}

lib.es5.d.ts 中没有什么像您的 Awaitable ,它们在其中定义 PromiseLike Promise

There's nothing like your Awaitable in lib.es5.d.ts where they define PromiseLike and Promise.

我想如果他们定义了一个,他们会在这些定义中使用它.

I'd think if they'd defined one, they would use it in those definitions.

旁注:根据这些定义,在您的 Awaitable 中使用 PromiseLike 而不是 Promise 可能是有意义的:

Side note: Based on those definitions, it probably makes sense to use PromiseLike rather than Promise in your Awaitable:

type Awaitable<T> = T | PromiseLike<T>;

这篇关于TypeScript中的等待类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 13:50