Created
June 11, 2013 16:24
-
-
Save thanpolas/5758331 to your computer and use it in GitHub Desktop.
Promises doodle
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
| function retProm() { | |
| var def = when.defer(); | |
| return def.resolve(); | |
| } | |
| console.log('one'); | |
| retProm().then(function(){console.log('two')); | |
| console.log('three'); | |
| // this will print: | |
| // | |
| // one | |
| // three | |
| // two |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Let me explain further the difference between our two examples. In yours, when you call
def.resolve,thenhas not yet returned. So the implementation must insert an extra tick before callingonFulfilled, to ensure thatthenreturns first.In my example, when I call
def.resolve,thenhas already returned, since an async operation has already happened. So the implementation does not have to insert an extra tick before callingonFulfilled, sincethenhas already returned.