0

I've been implementing some features in javascript using promises, but am confused about one thing, difference b/w these two

Promise.resolve()

.then(doSomething()) 

and

.then(() => doSomething())
Abhijeet Ahuja
  • 5,596
  • 5
  • 42
  • 50

3 Answers3

2

then expects a function. In the first case, you're passing to then the result of calling doSomething(), and in the second case you're actually passing a function that calls doSomething().

Those would be equivalent (assuming you don't need arguments or this):

Promise.resolve().then(doSomething)
Promise.resolve().then(() => doSomething())
Lucas
  • 4,067
  • 1
  • 20
  • 30
  • 1
    Thanks Lucas, that's what I was looking for – Abhijeet Ahuja Feb 20 '17 at 00:32
  • 1
    *"If doSomething does use this those would be equivalent:"* That's not correct. In `() => doSomething()`, `this` inside `doSomething` will be `undefined` (or the global object). – Felix Kling Feb 20 '17 at 00:35
  • @Lucas The patterns are not strictly equivalent – guest271314 Feb 20 '17 at 00:38
  • @FelixKling can you elaborate? `doSomething.bind(this)` and `() => doSomething()`, `this` inside the function will be whatever `this` is in the scope it was called. – Lucas Feb 20 '17 at 00:50
  • @guest271314 can you elaborate? – Lucas Feb 20 '17 at 00:50
  • 1
    Inside the *arrow* function `this` will be what it is outside. But not inside `doSomething` (called inside the arrow function). The equivalent to `doSomething.bind(this)` would be `() => doSomething.call(this)`. If a function is called "normally" (as `f()`), `this` is either `undefined` (strict mode) or the global object (unless the function is bound of course). It doesn't matter at all what `this` is in the environment the function is called. See MDN for more information about `this`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this – Felix Kling Feb 20 '17 at 00:51
  • @Lucas See http://stackoverflow.com/a/41194287/ – guest271314 Feb 20 '17 at 00:51
  • @FelixKling Ops, missed that one. Thanks. I'll edit my answer – Lucas Feb 20 '17 at 00:53
1

.then(doSomething()) immediately invokes doSomething before the then callback is called.

.then(() => doSomething()) creates a new function which becomes the then callback.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
0

To expand on @Lucas answer, which is correct, then in a Promise is formulated like this (from MDN):

Promise.then(onFulfilled[, onRejected]);

Promise.then(function(value) {
   // fulfillment
  }, function(reason) {
  // rejection
});

So your first argument is a function for fulfillment, and your second function an argument for a rejection, both arguments being functions, to which values are passed based on that which occurred.

So, as @Lucas said,

Promise.resolve().then(() => doSomething()) actually calls your function, where as just passing in doSomething() calls the function and returns a value or undefined if there is no return, which will do nothing unless that function returns a function (which would probably be an unreasonable way to go about a thenable).

thesublimeobject
  • 1,393
  • 1
  • 17
  • 22