This question already has an answer here:
- Handling async request with React, Redux and Axios? 4 answers
How to get normal JSON object from Promise Object in react-redux?
My action.js contains:
export function allEmp() {
let url = "employees?access_token=";
let result = ApiCall.getApiCall(url)
.then(function (response) {
return response;
})
.catch(function (error) {
return error;
});
console.log("result",result);
return {
type: "ALL_EMP",
payload: new Promise((resolve, reject) => {
setTimeout(() => {
resolve(result);
}, 2000);
})
};
}
my API call configuration is:
getApiCall(url) {
let base_url = "http://192.168.1.151:3000/api/";
let api_token = "1f7169e92c1d0722db575b877707cf0b88b8f0ad";
let fetch_url = base_url + url + api_token;
let myHeaders = new Headers({
'Accept': 'application/json',
'Content-Type': 'application/json'
});
return fetch(fetch_url, {
method: "GET",
headers: myHeaders
})
.then(function(response) {
if (response.ok) {
return response.json();
} else {
var error = new Error(response.statusText);
error.response = response;
throw error;
}
})
.then(function(json) {
return json;
})
}
My store.js :
import {createStore, combineReducers, applyMiddleware} from "redux";
import thunk from "redux-thunk";
import promise from "redux-promise-middleware";
import userDetails from "./reducers/User_reducer";
export default createStore(
combineReducers({
userDetails
}),
{},
applyMiddleware(thunk, promise())
);
But here I'm getting Promise object as a result. So that when I'm assigning in payload it is getting as undefined. How should I manipulate it when I'm sending the response as payload.
Can anyone give me some suggestion?