I am trying to make an API request based on the result of another API request. I got it working the way I want it but I don't like to use forEach for my API call and I can't wrap my head around how to get in done with a different method. Are there any suggestions on how I could do it in a more efficient way, eg. with promise.all?
here is my code:
api.js
export const fetchOrders = () => {
return fetch(`${url}/work_orders`).then(res => res.json());
};
export const fetchWorker = id => {
return fetch(`${url}/workers/${id}`).then(res => res.json());
};
app.js
function OrderReducer(state, action) {
if (action.type === "fetch") {
return {
orders: [],
error: null,
loading: true
};
} else if (action.type === "success") {
return {
...state,
orders: [
...state.orders,
{
order: action.order,
worker: action.worker
}
],
loading: false
};
} else if (action.type === "error") {
return {
...state,
error: action.message,
loading: false
};
} else {
throw new Error("no action type initialized");
}
}
const Orders = () => {
const [state, dispatch] = React.useReducer(OrderReducer, {
orders: [],
error: null,
loading: true
});
React.useEffect(() => {
dispatch({ type: "fetch" });
fetchOrders()
.then(({ orders }) => {
return orders;
})
.then(orders =>
orders.forEach(order => {
fetchWorker(order.workerId).then(({ worker }) =>
dispatch({ type: "success", order, worker })
);
})
)
.catch(({ message }) => dispatch({ type: "error", message }));
}, []);
archived output:
orders:[
0: {
order:{...},
worker:{...}
},
...
...
...
20: {
order:{...},
worker:{...}
}
]
I tried the following fetch
const fetchWorker = async order => {
const res = await fetch(`${url}/workers/${order.workerId}`);
const { worker } = await res.json();
return { order, worker };
};
const fetchOrders = async () => {
const res = await fetch(`${url}/work_orders`);
const { orders } = await res.json();
return Promise.all(orders.map(order => fetchWorker(order)));
};
fetchOrders()
.then(data => console.log(data))
.catch(({ message }) => console.log(message));
and it retuned what I was trying to get. an array, each object with a value of order and worker
[20]
[ { order:...., worker:... }, ...]
any suggestions on how to improve the code?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.