Created
December 30, 2016 19:27
-
-
Save fernandezpablo85/d020ea0efada7f5d4ab746f803ce723c 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
/* | |
problema de la entrevista que mencioné en el último podcast: | |
https://soundcloud.com/sin-humo/entrevistas-de-trabajo-y-111mil | |
la idea es crear la función 'deferred', que permita un comportamiento | |
similar a lo que serían promises de javascript. | |
el deferred puede 'resolverse' con un valor cualquiera, o con otro 'deferred'. | |
en el segundo caso, el próximo `then` debería "esperar" también a este. | |
en el programa de ejemplo a continuación, el output debería ser el siguiente: | |
- first callback resolved with A | |
* pausa de <TIMEOUT> | |
- second callback resolved with B | |
- third callback resolved with C | |
*/ | |
var d = new deferred(), TIMEOUT = 1000; | |
d.then(function (result) { | |
console.log('first callback resolved with', result); | |
return 'B'; | |
}); | |
d.then(function (result) { | |
var d1 = new deferred(); | |
setTimeout(function() { | |
console.log('second callback resolved with', result); | |
d1.done('C'); | |
}, TIMEOUT); | |
return d1; | |
}); | |
d.then(function (result) { | |
console.log('third callback resolved with', result); | |
return result; | |
}); | |
d.done('A'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment