【发布时间】:2018-09-02 09:47:11
【问题描述】:
我正在尝试开发一个应用程序,该应用程序在给定关键字的情况下显示来自 Unsplash 的照片。我设法使用 unsplash.js 获取特定的照片。在我的动作中,我有几个动作创建者:
export const fetchPhotos = () => ({
type: FETCH_PHOTOS
});
export const receivePhotos = term => {
const unsplash = new Unsplash({
applicationId:
"id",
secret: "secret",
callbackUrl: "callback"
});
console.log(term);
const response = unsplash.search
.photos(term, 1, 20)
.then(toJson)
.then(json => json)
.then(json => json)
console.log(response.then(results => results));
return {
type: RECEIVE_PHOTOS,
payload: response
};
}
export const unsplash = (term) => dispatch => {
console.log(term);
dispatch(fetchPhotos());
setTimeout(() => {
dispatch(receivePhotos(term));
console.log("dispatching")
return Promise.resolve();
}, 1000)
}
然后我的减速器会这样做:
const initialState = {
isFetching: false,
sortDirection: null,
sortKey: null,
items: []
}
export default function(state = initialState, action) {
switch (action.type) {
case FETCH_PHOTOS:
console.log(state, "Fetch photos reducer");
return {
...state,
isFetching: true
};
case RECEIVE_PHOTOS:
console.log("Receive photos reducer", action.payload)
return {
...state,
isFetching: false,
items: action.payload
};
case SET_SORT:
return {
...state,
sortKey: action.sortKey,
sortDirection: action.sortDirection
};
default:
return state;
}
}
但是,当 receivePhotos 操作创建者调用 API 时,我有一个需要解决的承诺,以便整个应用程序正常工作。我的 fetch photos reducer 是控制台记录操作,然后出现 Promise,但它始终处于待处理状态。然后我的 receivePhotos 动作创建者调度到 reducer,我可以看到这是一个 Promise:
我该如何解决这个承诺?
【问题讨论】:
标签: javascript redux redux-thunk