Skip to content

Instantly share code, notes, and snippets.

@andyhite
Last active May 12, 2022 15:03
Show Gist options
  • Save andyhite/7e4252434907012ce310eef3d6b97d2e to your computer and use it in GitHub Desktop.
Save andyhite/7e4252434907012ce310eef3d6b97d2e to your computer and use it in GitHub Desktop.
router.get("/orders/:id", (req, res) => {
const { sourceCode } = req.query;
try {
const order = await getOrderById(req.params.id);
res.status(200).json(order);
} catch (err) {
if (axios.isAxiosError(err)) {
if (err.code === 404 && sourceCode) {
await refreshOrderSource(sourceCode);
const order = await getOrderById(req.params.id);
res.status(200).json(order);
}
throw err; // whatever we do with generic errors
}
throw err;
}
});
// in the api lib
const refreshOrderSource = async (sourceCode: string, synchronous: true) => {
if (synchronous) {
// logic for polling and waiting for the order source to be refreshed goes here
// probably calling `refreshOrderSource` recursively with synchronous = false in each loop
// until it returns from this function when the order is done refreshing
}
// logic for initiating the refresh goes here
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment