简体   繁体   English

返回链式诺言的函数的返回类型

[英]Return type of a function that returns a chained promise

I have a class that returns a chained promise, first promise's type is angular.IPromise<Foo> , the second promise resolves with type angular.IPromise<Bar> . 我有一个返回链式诺言的类,第一个诺言的类型为angular.IPromise<Foo> ,第二个诺言的类型为angular.IPromise<Bar>

Why is it the return type of doSomething angular.IPromise<Bar> ? 为什么返回doSomething angular.IPromise<Bar>的返回类型?

I would have thought that the return type would be angular.IPromise<Foo> since that's the first thing that is returned by the function. 我本以为返回类型将是angular.IPromise<Foo>因为那是该函数返回的第一件事。 I am aware that then() returns a promise and will wrap what is returned by it into the response of that, but this is still confusing me. 我知道then()返回一个promise并将其返回的内容包装到那个响应中,但这仍然让我感到困惑。

import { Something } from '../somewhere';
import { Bar } from '../somewhereelse';

class Test {
    doSomething(): angular.IPromise<Bar> {
        return Something.getFoo() // getFoo() return type angular.IPromise<Foo>
            .then(() => {
                let x: Bar = {};        
                return x; 
            });
    }
}

Any help would be appreciated. 任何帮助,将不胜感激。 I am more than happy to give any more code if required. 如果需要,我很乐意提供更多代码。

First thing is : you don't return Something.getFoo() . 第一件事是:您不返回Something.getFoo() You return Something.getFoo().then(...) . 您返回Something.getFoo().then(...) Even if promises are used to execute asynchronous tasks, they are returned immediately. 即使使用诺言来执行异步任务,也会立即返回它们。

then() returning a angular.IPromise the returned promise is not the one from Something.getFoo() but the one returned by Something.getFoo().then(...) then()返回一个angular.IPromise返回的承诺不是Something.getFoo()那个,而是Something.getFoo().then(...)返回的Something.getFoo().then(...)

and as the callback provided to then() is returning a Bar object: promise.ten(foo=>return new Bar()) your statement actually returns a Promise<Bar> object. 并且提供给then()的回调返回一个Bar对象: promise.ten(foo=>return new Bar())您的语句实际上返回了Promise<Bar>对象。

as @TsatsuyukiIshi stated, this is the type definition of angular.IPromise.then : 如@TsatsuyukiIshi所述,这是angular.IPromise.then的类型定义:

then<X>(successCallback: (promiseValue: T) => IPromise<X>|X, ...): IPromise<X>

this method is generic, and its return type ( X ) depends on the type that is provided as parameter ( (promiseValue: T) => IPromise<X>|X ). 此方法是通用的,其返回类型( X )取决于作为参数提供的类型( (promiseValue: T) => IPromise<X>|X )。

If you replace X by Bar this is obvious: 如果用Bar替换X ,这很明显:

then(successCallback: (promiseValue: T) => IPromise<Bar>|Bar, ...): IPromise<Bar>

This returns angular.IPromise<Bar> , provided the code compiles. 如果代码可以编译,它将返回angular.IPromise<Bar>

Interfaces can map to any amount of classes, and they can be easily identified with the name prefixed with I . 接口可以映射到任意数量的类,并且可以使用前缀I的名称轻松标识它们。

The concrete type would be a detail of Promise implementation, however it satisfies IPromise<Bar> as long as it returns a Bar on resolve. 具体类型将是Promise实现的详细信息,但是只要它返回有关解析的Bar ,它就可以满足IPromise<Bar>

EDIT: TypeScript determines it via the template parameter in then<TResult>(successCallback: (promiseValue: T) => IPromise<TResult>|TResult, ...): IPromise<TResult> . 编辑:TypeScript通过then<TResult>(successCallback: (promiseValue: T) => IPromise<TResult>|TResult, ...): IPromise<TResult>的模板参数确定它。 This determines the return type of the callback statically. 这将静态确定回调的返回类型。 source 资源

Also see: Whats the difference between "declare class" and "interface" in TypeScript 另请参见: TypeScript中的“声明类”和“接口”之间有什么区别

If you need to return different types from the result then you can use any type. 如果需要从结果中返回不同类型,则可以使用any类型。 typescript is not 100% strongly typed language. 打字稿不是100%强类型语言。 The type can change at runtime. 类型可以在运行时更改。 Actually the parameter from the resolver just ignored and callback returned the type as expected. 实际上,解析器中的参数只是被忽略,回调函数按预期返回了该类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM