[英]Does catch block not get executed if 'onRejected' function is provided for 'then' block in a Promise?
[英]What is passed to the function in a Promise .catch block?
這兩個 styles 在 Promises 中處理.catch
塊的一般區別是什么:
...
.catch(e => myMethod(e))
...
.catch(myMethod)
Promise 的.catch
將什么傳遞給接收的方法?
例如,可以有額外的 arguments 嗎?
在這兩種情況下,只有一個論點。
There's no fundamental difference between these two styles, except that an arrow function behaves differently than a real function
, especially this
will be undefined
or window
(depending on whether strict mode is enabled or not) with a function
, and with an arrow function it's the this
與聲明它的上下文相同。
從這個MDN Catch 語法文檔:
這個
.catch
有一個參數:reason
:拒絕原因。
箭頭 function 表達式是常規 function 表達式的語法緊湊替代方案,盡管沒有與 this、arguments、super. 或 new.target 關鍵字的綁定。 Arrow function 表達式不適合用作方法,並且不能用作構造函數。
在catch(e => myMethod(e))
中,您傳遞了一個匿名 function ,它接受一個參數e
並調用myMethod(e)
在catch(myMethod)
中,您直接傳遞myMethod
而不是匿名 function (在上述情況下),它帶有一個參數e
。
所以,兩者都是一樣的。 而傳遞的參數e
就是被拒絕的“理由”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.