1

I wanna call a delete api for each selected customer. I am using one service layer. so when I call the service with the selected custoer code then the customer will be deleted.

In AngularJS I used $.allSettled which will give callbacks for fullfilled and not full filled. I can insert the success and failed customer deletions and I can show in UI about succeeded list and failed list.

 $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)
}
I implemented but I need to get all the success list as well as failed list. So that I can show in UI. which customers deletion failed and which are get success. I wanna maintain one array contain with objects which are having { custCode:100 , status: fail/success }

How to achieve this in angular2+

Abhi
  • 255
  • 3
  • 20
  • Just use the native `Promise.all([promise, ...])` implementation. – Brandon Taylor Oct 22 '18 at 20:18
  • @Brandon I implemented but I need to get all the success list as well as failed list. So that I can show in UI. which customers deletion failed and which are get success. I wanna maintain one array contain with objects which are having custCode:100 and status: fail/success. – Abhi Oct 22 '18 at 20:25
  • 2
    See this answer for some examples: https://stackoverflow.com/questions/31424561/wait-until-all-es6-promises-complete-even-rejected-promises – Brandon Taylor Oct 22 '18 at 20:30
  • @Brandon I will check out. Do I need to change anything in service so that it returns promise, or its fine? – Abhi Oct 22 '18 at 20:34
  • Yes, your service will need to return a promise, not an observable. – Brandon Taylor Oct 22 '18 at 20:44
  • @Brandon I am able to do this but one thing, I would like to know the payload sent when calling the promise. So that I can match with the received response and show the colors inside a table ( red, green ) etc – Abhi Oct 23 '18 at 09:32
  • If you need to keep track of which payload for which promise, you'll probably want to keep a separate object for that so you can compare them later. – Brandon Taylor Oct 23 '18 at 10:50
  • @Brandon Yes, I have that master list but response which is received dont have any information to map to the master data. – Abhi Oct 23 '18 at 13:03
  • You don't have any unique identifiers in your response you could use to map back to your payload(s) object? – Brandon Taylor Oct 23 '18 at 14:29
  • @Brandon sorry , I didn't understand. Can you please elaborate with an example. Thanks – Abhi Oct 24 '18 at 05:29
  • @Brandon "You don't have any unique identifiers in your response you could use to map back to your payload(s) object? " Ans: To associate request and response I dont have any unique identifier. – Abhi Oct 24 '18 at 11:31
  • 1
    You might need to adjust your request payload and response so you have something to coordinate them on, since there's no guarantee that the promises will complete in a specific order. – Brandon Taylor Oct 24 '18 at 13:06

0 Answers0