Skip to content

Instantly share code, notes, and snippets.

@maxjing
Last active March 28, 2019 19:48
Show Gist options
  • Save maxjing/b11071929b7b5a2a1c8b7b3bc77f8c40 to your computer and use it in GitHub Desktop.
Save maxjing/b11071929b7b5a2a1c8b7b3bc77f8c40 to your computer and use it in GitHub Desktop.
order of promise
function get(url) {
return new Promise((resolve, reject) => {
fetch(url)
.then(res => res.json())
.then(data => resolve(data))
.catch(err => reject(err));
});
}
#example
app.use("/reports", (req, res) => {
const type = req.query.type;
let timestamp;
if (req.query.timestamp) {
timestamp = req.query.timestamp;
} else {
timestamp = utils.getMostRecentTimestamp(type);
}
Promise.all([
get(
`http://localhost:5001/report?type=${type}&page=hp&timestamp=${timestamp}`
),
get(
`http://localhost:5001/report?type=${type}&page=srp&timestamp=${timestamp}`
),
get(
`http://localhost:5001/report?type=${type}&page=ldp&timestamp=${timestamp}`
)
])
.then(([hp, srp, ldp]) =>
res.json({
hp: hp,
srp: srp,
ldp: ldp
})
)
.catch(err => res.send(err));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment