Last active
May 27, 2020 16:38
-
-
Save vituchon/0772d19f890850f233bc79c1572b4535 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
//Esto no funciona.... | |
// wait ms milliseconds | |
function wait(ms) { | |
return new Promise(r => setTimeout(r, ms)); | |
} | |
function f() { | |
const promise = new Promise((resolve, reject) => { | |
setTimeout(() => resolve("done!"), 2000) | |
}); | |
var ret = undefined; | |
promise.then((r) => { | |
console.log("then") | |
ret = r; | |
}).catch(() => { | |
ret = null; | |
}) | |
// un intento de polling sobre la promesa, que claramente no funca, pues las promesas de espera (wait 500) son promesas y no sleeps verdaderos.. | |
var attemps = 0 | |
while (ret === undefined && attemps < 20) { | |
console.log("waiting") | |
const doSomething = async () => { | |
await wait(500) | |
//do stuff | |
} | |
doSomething() | |
attemps++ | |
} | |
console.log("END") | |
console.log(ret) | |
return ret | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment