-1

Can someone explain why this prints in reverse order?

Code:

when('test')
  .then(function() {console.log('should be first');})
  .then(console.log('should be second'));

Output:

should be second
should be first

PS: I am using when.js version: when@3.4.3

gromiczek
  • 2,970
  • 5
  • 28
  • 49
Anastasios Andronidis
  • 6,310
  • 4
  • 30
  • 53

1 Answers1

5

You're immediately executing the second console.log, and passing the return value to then. You need to pass functions to then.

You've effectively done this:

var x = console.log('should be second')

when('test')
  .then(function () { console.log('should be first'); })
  .then(x);
user229044
  • 232,980
  • 40
  • 330
  • 338
  • 1
    It's worth mentioning - that /A+ Promise libraries like WhenJS guarantee that this is _always_ the order things happen. in some promise implementations you do not have that guarantee (that code in `.then` is always run async) – Benjamin Gruenbaum Aug 06 '14 at 05:35