Skip to content

Instantly share code, notes, and snippets.

@vituchon
Last active May 27, 2020 16:38
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 vituchon/0772d19f890850f233bc79c1572b4535 to your computer and use it in GitHub Desktop.
Save vituchon/0772d19f890850f233bc79c1572b4535 to your computer and use it in GitHub Desktop.
//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