Last active
June 18, 2019 06:00
-
-
Save ykiu/885a9d814246b919db2e64e3ef7f766d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 defered() { | |
const def = {}; | |
def.promise = new Promise((resolve, reject) => {def.resolve = resolve; def.reject = reject;}); | |
return def; | |
} | |
function request(url) { | |
let def = defered(); | |
const req = new XMLHttpRequest(); | |
req.open("GET", "http://localhost:8000/DSC00887.JPG"); | |
function update(event) { | |
const oldDef = def; | |
def = defered(); | |
oldDef.resolve({ next: event.type === 'load' ? null : def.promise, event }); | |
} | |
req.addEventListener('progress', update); | |
req.addEventListener('load', update); | |
req.upload.addEventListener('progress', update); | |
req.upload.addEventListener('load', update); | |
req.send(); | |
return { next: def.promise, event: null }; | |
} | |
async function main() { | |
let { next, event } = request(); | |
do { | |
const value = await next; | |
next = value.next; | |
event = value.event; | |
console.log(event); | |
} while (next); | |
console.log('COMPLETE') | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment