簡體   English   中英

在 Promise.catch 塊中傳遞給 function 的是什么?

[英]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拒絕原因。

從這個MDN 箭頭 Function 文檔

箭頭 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM