$ q.allsettled相当于2的角度来捕捉所有成功和失败的情况

时间:2018-10-22 20:16:59

标签: angular es6-promise q

我想为每个选定的客户调用一个删除API。我正在使用一个服务层。 因此,当我使用选定的客户代码致电该服务时,该客户将被删除。

在AngularJS中,我使用了$ .allSettled,它将为完全填充和未完全填充提供回调。我可以插入成功和失败的客户删除项,并且可以在UI中显示有关成功列表和失败列表的信息。

 $q.allSettled(promises)
                .then(function(results) {
                    results.forEach(function(result) {
                        if (result.state === 'fulfilled') {
                            // success :)

                          
                        } else {
                            // failed :(

                           //result.reason.config.data contains api input data
                        }
                    });
                }).finally(function() {
                    
                });

this.promises = [];
//FORMING LIST OF PROMISES
this.customerList.forEach(code => {
     this.promises.push(this.customersservice.delete(code));
});


Promise.all(this.promises).then(values => { 
  console.log(values);
}).catch(reason => { 
    //here i need to catch every time an api call fails for some reason , so that I can show in UI
  console.log(reason)
});


//SERVICE
 delete(code: string): Observable<any> {
    return this.http.delete(`${this.baseURL}/` + code)
}
我实现了,但是我需要获取所有成功列表以及失败列表。这样我就可以在UI中显示。哪些客户删除失败,哪些成功。我想维护一个包含{{custCode:100,status:fail / success}

的对象的数组

如何在angular2 +中实现这一目标

0 个答案:

没有答案