6
'use strict';

Promise.resolve(() => 'John')
  .then((args) => {
    console.log(args);
    throw new Error('ops')
  })
  .catch((ex) => {
    console.log(ex)
  })
  .then(() => {
    throw new Error('ups')
    console.log('Doe')
  })

I think console.log(args); should output 'John', but when I run this code, the output is [ [Function] ]

So I am confused.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
BlackMamba
  • 10,054
  • 7
  • 44
  • 67

3 Answers3

4

Promise.resolve will create a new Promise resolved with the value you pass to it. So, in your case, your promise is actually resolved with the function object. It means that, the then handler is passed the function object itself.

What you should have done is

new Promise((resolve, reject) => resolve('John'))
  .then(args => {
    console.log(args);
    throw new Error('ops')
  })
  .catch(console.log.bind(console));

Now, you are creating a Promise object and you are resolving that with the value John.


If you want your Promise to be resolved with a value readily, then don't pass the function object but pass the actual value itself to Promise.resolve function.

Promise.resolve('John')
  .then(args => {
    console.log(args);
    throw new Error('ops')
  })
  .catch(console.log.bind(console));

Now, you have a Promise, resolved with value John and the then handler will get the resolved value John.

Note: This is the recommended way of creating a Promise when you know the actual way to resolve with readily, so that you can avoid the Promise constructor anti-pattern.

Community
  • 1
  • 1
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • Alternatively: `Promise.resolve().then( () => "John" ).then( (args) => console.log(args) ...` – aaaidan Jan 08 '16 at 02:05
  • @aaaidan You cannot invoke the `Promise` constructor without passing a function object to it. – thefourtheye Jan 08 '16 at 02:07
  • 1
    @thefourtheye `(args) => ` and `(ex) => ` can be reduced to `args => ` n `ex => ` – mido Jan 08 '16 at 02:25
  • It's worth noting that the result of a non-blocking function can be a function too and thus a function is a valid `Promise` value. –  Jul 10 '16 at 18:18
3
'use strict';

Promise.resolve('John')
  .then((args) => {
    console.log(args);
    throw new Error('ops')
  })
  .catch((ex) => {
    console.log(ex)
  })
  .then(() => {
    throw new Error('ups')
    console.log('Doe')
  })

I modify Promise.resolve('John'), it works. Please see the Promise.resolve.

BlackMamba
  • 10,054
  • 7
  • 44
  • 67
1

resolve is used to pass the arguments directly to the then-handler

if you want 'John', you need to call the anonymous function in your call to resolve()

Promise.resolve(function(){return 'John';}());

notice the }() function call.

activedecay
  • 10,129
  • 5
  • 47
  • 71
  • And how is this different from `Promise.resolve('John')`? –  Jan 08 '16 at 03:43
  • Of course it has no effect on the result passed to the "then"; however, I'm sure it's obvious that we have a useless function call getting in the way. Clearly my point was to illustrate what's going on when the OP sees [Function]. – activedecay Jan 08 '16 at 05:59