A
A
aimpmario2015-06-12 21:37:20
JavaScript
aimpmario, 2015-06-12 21:37:20

Reject from switch. The log is reject'a is not present. How to reject from switch?

Dear gurus, maybe someone can answer my questions.

return new Promise(function(resolve, reject){
      
      Object.keys(schemaPaths).forEach(function(path){        
        switch(schemaPaths[path].instance){
          case 'String' :
            cs(schemaPaths[path], data[path]).catch(function(e){
              console.error(e);
              reject(e);
            }); 
          break;
            
          default : 
              reject('default');
              break;
        };
      });
    
      resolve()
 });

The function csreturns a Promise, more precisely Promise.reject(). Then it displays an error in the console and does further reject.
The problem starts here. The log is written to the console, and reject is not executed inside case 'String' :, but for some reason it first goes to default.and there it will already execute reject().
This was question number 1.
Further more. If you remove
default : 
              reject('default');
              break;
};

There will be something like this:
return new Promise(function(resolve, reject){
      
      Object.keys(schemaPaths).forEach(function(path){        
        switch(schemaPaths[path].instance){
          case 'String' :
            cs(schemaPaths[path], data[path]).catch(function(e){
              console.error(e);
              reject(e);
            }); 
        };
      });
    
      resolve()
 });

And here the same thing, (Log - yes, reject - no). Only this time resolve() will execute.
I rewrote everything for If-Elseeverything and it works as it should. But the question remains, but there is no solution.
Perhaps someone knows what this behavior is connected with, and how it can be cured?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
_
_ _, 2015-06-12
@aimpmario

return new Promise(function(resolve, reject){
      
      Object.keys(schemaPaths).forEach(function(path){        
        switch(schemaPaths[path].instance){
          case 'String' :
            return cs(schemaPaths[path], data[path]).catch(function(e){
              console.error(e);
              reject(e);
            }); 
          break;
            
          default : 
              return reject('default');
              break;
        };
      });
    
      resolve()
 });

UPD. Looks like a late hour affects, inattentively read the code.
function wtf(){   
      var promise;   
      Object.keys(schemaPaths).forEach(function(path){        
        switch(schemaPaths[path].instance){
          case 'String' :
            if(!promise){ 
              promise = cs(schemaPaths[path], data[path]).catch(function(e){
                console.error(e);
                return Promise.reject(e);
              });
            }  
          break;
            
          default : 
              if(!promise) promise = Promise.reject('default');
              break;
        };
      });
    
      return promise;
}

But overall it's kind of bullshit. What does this code do? Only the first iteration of Object.keys(schemaPaths[path]) will be valid, because after it, the promise will be set anyway, and all other iterations will be wasted.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question