Created
July 15, 2019 15:40
-
-
Save wzc0x0/ef3018c9ab664e8b19d0de8399173d7a to your computer and use it in GitHub Desktop.
ajax order by request time
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
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