Skip to content

Instantly share code, notes, and snippets.

@wzc0x0
Created July 15, 2019 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wzc0x0/ef3018c9ab664e8b19d0de8399173d7a to your computer and use it in GitHub Desktop.
Save wzc0x0/ef3018c9ab664e8b19d0de8399173d7a to your computer and use it in GitHub Desktop.
ajax order by request time
const url = "https://randomuser.me/api/?results="
let count = 0
let results = []
let cbCount = 0
while (count < 5) {
count++
asyncF(count)
}
function asyncF(c) {
fetch(url+c)
.then(res => res.json())
.then(res => Promise.reject(res))
.catch(err => {
results.push({order: c,res:err})
cb()
})
}
function cb() {
if (cbCount === 4) {
results = results.sort((a,b) => a.order - b.order).map(item => item.res)
console.log(results)
} else {
cbCount++
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment