Skip to content

Instantly share code, notes, and snippets.

@soapdog
Last active March 12, 2016 20:41
Show Gist options
  • Save soapdog/0130b10a84e9cd5328f8 to your computer and use it in GitHub Desktop.
Save soapdog/0130b10a84e9cd5328f8 to your computer and use it in GitHub Desktop.
Gambiarra de Promise.
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())");
@soapdog
Copy link
Author

soapdog commented Mar 12, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment