Last active
March 12, 2016 20:41
-
-
Save soapdog/0130b10a84e9cd5328f8 to your computer and use it in GitHub Desktop.
Gambiarra de Promise.
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
function makePromise() { | |
return new Promise(function(resolve, reject){ | |
setTimeout(function(){ resolve('hey!') }, 500); | |
}); | |
} | |
var gambiwrap = function(p) { | |
console.log("Promessa não resolvida"); | |
return p.then(function(r) { | |
gambiwrap = function() { | |
console.log("Promessa resolvida"); | |
return r; | |
} | |
}) | |
} | |
console.log("primeira chamada...") | |
gambiwrap(makePromise()); | |
console.log("Agora chama vc gambiwrap(makePromise())"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Quando chama o primeiro:
p = gambiwrap(makePromise());
Ele retorna uma promise. Depois que essa promise resolve, qualquer chamada para:
p = gambiwrap(makePromise());
Retorna o valor resolvido pela primeira vez.