gpt4 book ai didi

javascript - Redux thunk - 嵌套调度函数/ Action

转载 作者:行者123 更新时间:2023-11-30 19:07:53 25 4
gpt4 key购买 nike

我正在使用 redux-thunk 在我的 React 应用程序中执行异步操作,如下所示:

export const fetchImages = (objects) => dispatch => {
const promises = objects.map(obj => axios
.get(`${API_URL}/files/${obj.img ? vendor.img : 'default.png'}`, {responseType: 'arraybuffer'})
.then( res => obj.imgData = 'data:;base64,' + convertArrayBufferToBase64(res.data))
);
return Promise.all(promises).then (() => Promise.resolve(objects));
}

当我在我的任何组件中使用它时,它工作得很好。但是,如果我像这样在另一个 Action 中使用它:

export const fetchAllObjects = () => dispatch => axios.get(`${API_URL}/objects?limit=50`)
.then(res => fetchImages(res.data.docs).then(objects =>
dispatch({
type: FETCH_ALL_OBJECTS,
payload: objects
});
));

它失败了。我希望它返回一个 promise ,但是它返回“dispatch => ...”,因此 then() 在返回值上失败。

最佳答案

我刚刚注意到 fetchImages 是一个返回函数的函数:

export const fetchAllObjects = () => dispatch => axios.get(`${API_URL}/objects?limit=50`)
.then(res => fetchImages(res.data.docs)(dispatch).then(objects =>
dispatch({
type: FETCH_ALL_OBJECTS,
payload: objects
});
));

关于javascript - Redux thunk - 嵌套调度函数/ Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58793625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com