Skip to content

Instantly share code, notes, and snippets.

@thanpolas
Created June 11, 2013 16:24
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 thanpolas/5758331 to your computer and use it in GitHub Desktop.
Save thanpolas/5758331 to your computer and use it in GitHub Desktop.
Promises doodle
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
@domenic
Copy link

domenic commented Jun 11, 2013

Let me explain further the difference between our two examples. In yours, when you call def.resolve, then has not yet returned. So the implementation must insert an extra tick before calling onFulfilled, to ensure that then returns first.

In my example, when I call def.resolve, then has already returned, since an async operation has already happened. So the implementation does not have to insert an extra tick before calling onFulfilled, since then has already returned.

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