1

I found myself lack of understanding with ES6 Promise.

I have two questions

Question 1)

// clear

var _o = null

var m = () => {
    return new Promise(k => {
        _o = k
    })
}

m().then(r => {console.log(r+1000)})

_o(100)

// 1100

As you've seen above, when _o is called with its value, promise resolves, and we can log the value (thanks to .then)

So the above example looks very clear.

But here is an example that I cannot fully understand.

// weird 

var _o = null

var test = () => {
    return new Promise(res => {
        _o = res
    })
}

test();

var p = _o(100)

console.log(p) // undefined

How can p be undefined?

According to the promise mdn documentation,

The Promise.resolve() method returns a Promise object that is resolved with a given value.

I thought that p has to be resolved Promise object.

Question 2)

Promise.resolve(10).then(r => console.log(r))  // 10


var p = Promise.resolve

p(10).then(r => console.log(r))

// Uncaught TypeError: PromiseResolve called on non-object

why does that p(10) fires an error?

I already referenced this article, but still failed to fully understand.

Thanks in advance for your help!

jwkoo
  • 1,518
  • 2
  • 13
  • 24

1 Answers1

2

How can p be undefined?

You seem to make the assumption that Promise.resolve (which you quote) is the same as the function that gets passed into the Promise constructor callback. They are not the same:

new Promise(resolve => {
   console.log(Promise.resolve === resolve);
})

In fact, the specification defines that the passed in function returns undefined: 25.6.1.3.2 Promise Resolve Functions.

You can think of Promise.resolve being equivalent to

Promise.resolve = function (value) {
  return new Promise(resolve => resolve(value));
}

why does that p(10) fires an error?

This is only partly related to Promise. It generally is due how this inside functions work. From the specification about Promise.resolve:

The resolve function expects its this value to be a constructor function that supports the parameter conventions of the Promise constructor.

If you call the function as p(...) then this is undefined.

If the function is called as Promise.resolve(...) then its this value will refer to Promise.

It's not uncommon for builtin functions to expect their this value to be a specific kind of object/value.

Bergi
  • 513,640
  • 108
  • 821
  • 1,164
Felix Kling
  • 705,106
  • 160
  • 1,004
  • 1,072
  • @Bergi: Thanks! *"Built-in functions that are ECMAScript function objects must be strict functions."* https://www.ecma-international.org/ecma-262/10.0/#sec-built-in-function-objects – Felix Kling Nov 04 '20 at 16:41
  • Very helpful. Appreciate for your help! – jwkoo Nov 04 '20 at 19:27
  • @Bergi I have further question regards to the first question. If p returns undefined, then how .then knows whether the promise has been resolved? – jwkoo Nov 04 '20 at 19:48
  • @jwkoo The `resolve()` call *tells* the promise to become resolved. – Bergi Nov 04 '20 at 20:00
  • @Bergi The resolve() call returns undefined, but set flag to true so that .then to be chained for example? – jwkoo Nov 04 '20 at 20:04
  • 1
    @jwkoo It returns `undefined`, but schedules the callbacks that the `then()` calls registered on the promise. And it sets an internal flag, yes. – Bergi Nov 04 '20 at 20:09
  • @Bergi totally understood. Thank you! – jwkoo Nov 04 '20 at 20:10