私は最初の例がなぜ機能するのか混乱していますが、2番目の例はなぜそうではありません。私はそれがjsonを呼び出すと関係があると信じて、javascriptオブジェクトへの応答を解決しますか?だから、それはその機能に置かなければならない約束を返すのだろうか?私は3番目の例で投げられたエラーのためにこれを得ます。 #jsonは何を正確に行いますか?約束は#thenと#jsonでどのように機能しますか?
export const promiseErrorMiddleware = store => next => action => {
const url = action.url
const fetchName = action.fetchName
return Promise.resolve(fetch(url)).then((response) => {
return response.json()
}).then((data) => {
store.dispatch({data: data, needDirection: true, fetchName: fetchName })
})
}
//works
export const promiseErrorMiddleware = store => next => action => {
const url = action.url
const fetchName = action.fetchName
return Promise.resolve(fetch(url)).then((response) => {
store.dispatch({data: response.json(), needDirection: true, fetchName: fetchName })
})
}
//doesn't work
export const promiseErrorMiddleware = store => next => action => {
const url = action.url
const fetchName = action.fetchName
return Promise.resolve(fetch(url)).then((response) => {
console.log(resopnse.json())
return response.json()
}).then((data) => {
store.dispatch({data: data, needDirection: true, fetchName: fetchName })
})
}
//throws error
'response.json()'は**約束**を返します。前回の例では、 'resopnse.json()'にタイプミスがあります。それがエラーの原因かもしれません。 –
最初の例と3番目の例の違いは何ですか? 'Promise.resolve()'を使う目的は何ですか? – guest271314