如何使用和不使用数据返回成功和错误承诺?

8 javascript angularjs typescript

我想output在$ http调用之前或之后返回一个promise和一个对象.有人可以告诉我如何使用AngularJS框架做到这一点,并且对Typescript非常重要,所以我可以确定它是否正常工作?

 topicNewSubmit = (): ng.IPromise<any> => {
    var self = this;
    var myData1 = { abc: 123 }

    if (self.abc = 22) {
        // How can I return an OKAY promise from here?
    }
    if (self.abc = 33) {
        // How can I return an OKAY promise with myData1 from here?
    }
    if (self.abc = 88) {
        // How can I return a FAIL promise from here?
    }

    return self.$http({ url: self.url, method: "GET" })
        .then(
            (response: ng.IHttpPromiseCallbackArg<any>): any => {            
            var myData2 = { abc: 245 }
            // How can I return a promise and myData2. 
            // return(myData2) gives an error with Typescript

            // How can I return a promise and no data
            // return gives an error with Typescript 
        },
        (error: ng.IHttpPromiseCallbackArg<any>): ng.IPromise<any> => {
            var myData3 = { abc: 345 }
            // Is this the correct way to return an unhandled reject with myData3
            return self.$q.reject(myData);
        });
  }
Run Code Online (Sandbox Code Playgroud)

tho*_*aux 5

编辑:修复了代码并添加了一个TypeScript Playground 示例。方法输入正确,您可以通过抛出的输入错误来验证这一点,尝试修复它们;)。我从 angular 定义文件中复制了非常基本的必需接口。

编辑 #2这是上面 TypeScript Playground 示例的固定版本

如果我正确理解您的问题,您正在尝试为服务方法定义返回类型,声明它返回一个承诺,其解析结果将是某个对象

在这种情况下,您就快到了,我将您的两个示例方法拆分为不同的块,因为它们需要不同的方法。

一般而言,self = this当您使用粗箭头方法时,我删除了作用域复制 ( ),它会自动将方法的作用域限定到外部词法作用域。简而言之,没有必要进行范围复制,事实上,在您的示例中 self 并不总是指向服务(因为您是在方法内部而不是外部复制范围)。

另外,请注意 Angular 承诺的定义(截断):

interface IDeferred<T> {
    resolve(value?: T): void;
    reject(reason?: any): void;
}
Run Code Online (Sandbox Code Playgroud)

因此,输入 Angular Promise 只会为 Promise 的解析情况添加类型,而不是为被拒绝的情况添加类型。因此,在调用您的服务时,它会验证成功处理程序中的结果是否属于您定义的类型,但错误处理程序中的参数类型是否属于 type any

主题测试

要使此方法起作用,您需要注入$q您的服务,然后使用它来创建您自己的服务deferred

topicTest = (): IPromise<Foo> => { // return a promise which will result in a parameter of MyType upon resolving
    var deferred = this.$q.defer<Foo>(); // Type the deferred to get better 'intellisense' support
    if (this.abc = 99) {
        deferred.resolve(new Foo());
    }
    if (this.abc = 88) {
        deferred.reject("You can pass in whatever you like to the reject case");
    }
    return deferred.promise;
};
Run Code Online (Sandbox Code Playgroud)

话题新提交

$http已返回的承诺,所以你只需要通过附加一个挂接到这些then回调并返回从该法允许链接其他thencallabacks它。

在这种情况下,您的服务方法的返回类型将是angular.IPromise<() => any>您可以替换any为您想要的类型的地方。该then方法的返回类型必须对应于您在服务方法的返回类型中为通用占位符选择的任何类型。

topicNewSubmit = () : IPromise<Foo> => {
    return this.$http({ url: this.url, method: "GET" }).then((response): Foo => {
        return new Foo();
    }, (error) => {
        return "whatever you'd like, it does not have to correspond to Foo";
    });
}
Run Code Online (Sandbox Code Playgroud)

然后你可以使用你的服务

MyService.topicNewSubmit().then((data) => {
    // data needs to be of type T, where T is the type you defined in the generic placeholder of IPromise<T>
}, (error: any) => {
    // In the error case, the parameters are of type any
});
Run Code Online (Sandbox Code Playgroud)