Last active
March 28, 2019 19:48
-
-
Save maxjing/b11071929b7b5a2a1c8b7b3bc77f8c40 to your computer and use it in GitHub Desktop.
order of promise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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×tamp=${timestamp}` | |
), | |
get( | |
`http://localhost:5001/report?type=${type}&page=srp×tamp=${timestamp}` | |
), | |
get( | |
`http://localhost:5001/report?type=${type}&page=ldp×tamp=${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