3
我有測試代碼!承諾,獲取,codestyle。請解釋。然後(checkStatus)。然後(parseJSON)
function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return Promise.resolve(response)
} else {
return Promise.reject(new Error(response.statusText));
}
}
function parseJSON(response) {
return response.json()
}
我可以寫喜歡這裏:
function load(id) {
return fetch('/api/test/'+ id + '/', {method: 'get'})
.then(response => checkStatus(response))
.then(response => parseJSON(response))
.catch(error=>console.error(error))
或者我可以這樣寫有:
function load(id) {
return fetch('/api/test/'+ id + '/', {method: 'get'})
.then(checkStatus)
.then(parseJSON)
.catch(error=>console.error(error))
請解釋第二個變體。 .then(checkStatus) .then(parseJSON)
如何工作?
我只寫了函數的引用,並沒有運行它。
@oksana但是,有微妙的差異在這個'''this'''指向的兩個變體之間將指向你傳遞給'''然後''' – balajisoundar
@balajisoundar函數的內部 - 這就是爲什麼我使用了* think * * A'* as * 'B'。而不是說'A' *就像*'B' –
只是指出了差異。 – balajisoundar