On angular’s promise

  node.js, question

There is an interface that updates one field of the database at a time.

After sending multiple ajax requests in a circular inside, and after all these requests are successful, do something later.

The definition of success is: no exception is thrown andstate === "SUCCESS"

Excuse me, if you are instate ! == "SUCCESS"That is, when the modification is unsuccessful, it can make$q.all(promises)hit the target
Error callback execution?

For example:

var toModifyAttr = {
 Title: "New Title",
 Desc: "New Description",
 Price:199 // New Price
 bracket
 
 var promises = [];
 
 
 for( var attr in toModifyAttr ) {
 
 var promise = $http( params ).then(function( result ){
 
 if( result.state === "SUCCESS" ) {
 
 Log ("modified" plus attr plus "attribute succeeded");
 
 }else{
 
 Log ("failed to modify" plus attr plus "attribute");
 
 bracket
 }).catch(function( e ) {
 
 Log ("failed to modify" plus attr plus "attribute");
 
 console.log(e);
 
 })
 
 promises.push(promise);
 
 bracket
 
 //Make sure everything is saved successfully.
 //if no exception is thrown, but the modification fails, i.e. state is not "SUCCESS", how can the error callback be executed?
 $q.all(promises).then(function(){
 
 console.log('all done!  !'  );
 
 //Let's do some work after successful modification
 //Remove loading Mask
 //   ...
 //   ...
 
 },function(){
 
 //this error callback, if state!  =="SUCCESS "will also be executed.  How?
 Log ('not all successful');
 
 })

What you need at this time isinterceptors, the specific approach can be like this:

//Add interceptor:
 app.factory('StateInterceptor', function ($q)
 bracket
 return {
 response: function (response)
 bracket
 if(response.data.state !  == 'SUCCESS'){
 return $q.reject(response);
 bracket
 return response;
 bracket
 bracket
 });
 
 //configure interceptor to app
 app.config(function ($httpProvider)
 bracket
 $httpProvider.interceptors.push('StateInterceptor');
 });

Then your code can be changed to read as follows:

var toModifyAttr = {
 Title:' new title',
 Desc:' New Description',
 Price: 199 // New Price
 bracket
 
 var promises = Object.keys(oModifyAttr).map(key => $http(params));
 
 $q
 .all(promises)
 .then(function() {
 console.log('all done!  !'  );
 //Let's do some work after successful modification
 //Remove loading Mask
 //   ...
 //   ...
 })
 .catch(function() {
 console.log('errors here');
 });