2017-04-12 89 views
1

我想弄明白這一點。我有這樣的承諾Javascript抓住然後承諾

function Function1() { 
    return fetch() 
    .then((xx) =>) 
    .catch(error => throw(error)); 
} 

在另一個文件中使用此Function1承諾。

Function1() 
.then((xx) =>() 
.catch((error) => { 
    console.log('I want to Catch that stupid error here'); 
}); 

爲什麼我不能從捕獲錯誤中的Function1承諾中拋出錯誤消息,我在調用此Function1()?

您的任何種類的幫助和建議,我將不勝感激,親切:)

+0

一開始'.catch(錯誤=>擲(錯誤));'是多餘的,並不需要它,擺脫它 - 無效語法反正 –

+1

'。然後((xx)=>()' - 缺少一些東西? –

+0

你有一個缺失的「)」Function1()。然後(xx =>())。catch((error)=> {console.log('我想趕上這個愚蠢的錯誤');}); –

回答

3

使用throw.then功能。

// Here is Promise then throw example 
 

 
new Promise((resolve, reject) => { 
 
    resolve(5); 
 
}).then(result => { 
 
    throw 'Err'; 
 
}) 
 
.catch(error => { 
 
    console.log(error); 
 
    throw error; 
 
});