2017-08-15 29 views
1

有一個在我的代碼promice:解構promice回調

req.getValidationResult() 
     .then(result => { 
      let errors = result.array(); 
      if (errors.length) { 
       return res.status(400).json({ errors: errors }); 
      } 

      return next(); 
     }); 

我想知道是否有任何變種,以解構我的「結果」變量上,然後調用(像.then({result.array(): errors} =>...),而不是使let errors = result.array();分配。

回答

2

我相信這是不可能的,以天然的承諾。您可以稍微改變這段代碼:

req.getValidationResult() 
     .then(({array}) => array()) 
     .then(errors => { 
      if (errors.length) { 
       return res.status(400).json({ errors: errors }); 
      } 
      return next(); 
     }); 

或者您可以使用藍鳥。它有一個call方法:

req.getValidationResult() 
     .call('array') 
     .then(errors => { 
      if (errors.length) { 
       return res.status(400).json({ errors: errors }); 
      } 
      return next(); 
     }); 

但在情況下,你需要你的承諾轉換爲藍鳥承諾