1

My understanding of a Promise object is as follows:

var Promise = { 
    then: function() { ... },
    catch: function() { ... }
};

If I have this code below that has a function and returns a Promise object (fetch.js):

var xhr = require('xhr')

module.exports = function (uri) {
  return new Promise(function (resolve, reject) {
    xhr(uri, function (err, res, body) {
      if (err) return reject(err)
      if (res.statusCode !== 200) return reject(new Error(body))
      resolve(body)
    })
  })
}

Then in my index.js I do:

var fetch = require('./fetch');
var promise = fetch("some_url");

How is the structure of the promise object returned from var promise = fetch("some_url"); formed?

In the fetch.js in the new Promise(...) part you are passing in a function to a constructor. I haven't seen anything like this before and am wondering how the resolve and reject parameters in the new Promise(...) part get passed to the then and catch keys in the sample Promise object above.

Tholle
  • 108,070
  • 19
  • 198
  • 189
  • 1
    [ECMAScript 2015, 25.4 Promise Objects, ff.](http://www.ecma-international.org/ecma-262/6.0/#sec-promise-objects) – Andreas Feb 17 '17 at 07:07
  • So it says there that `A PromiseCapability is a Record value used to encapsulate a promise object along with the functions that are capable of resolving or rejecting that promise object.` But how do the "the functions that are capable of resolving or rejecting" get extracted from the `new Promise (resolve, reject) { ... })` constructor? –  Feb 17 '17 at 07:12
  • 2
    please see this post https://www.promisejs.org/implementing/ and linked stackoverflow question http://stackoverflow.com/questions/23772801/basic-javascript-promise-implementation-attempt/23785244#23785244 – Nikolay Lukyanchuk Feb 17 '17 at 07:12
  • Hmm ok I will try to understand that. –  Feb 17 '17 at 07:17

1 Answers1

4

i cant explain but show you example how then function get the resolved value or catch get error

take a variable a save promise to that variable and then execute then function like bellow example

var a = new Promise(function (resolve, reject) {
    resolve(1);
})
a.then();

when you execute a.then() it get two parameters PromiseStatus and PromiseValue in promiseStatus you will get it is resolved or reject and in PromiseValue you will get the value you passed with resolve or reject handler

Promise
__proto__
:
Promise
catch:catch()
constructor: Promise()
then:then()
Symbol(Symbol.toStringTag):"Promise"
__proto__:Object[[PromiseStatus]]:"resolved"[[PromiseValue]]:1
Ashish Patel
  • 921
  • 1
  • 10
  • 26
  • See also [What does \[\[PromiseValue\]\] mean in javascript console and how to do I get it](http://stackoverflow.com/questions/28916710/what-does-promisevalue-mean-in-javascript-console-and-how-to-do-i-get-it) – guest271314 Feb 17 '17 at 08:13
  • promise value will get you as a parameter in function of then like this a.then(function(res){ }) in res you will get the PromiseValue – Ashish Patel Feb 17 '17 at 08:33
  • So essentially the .then method is just there (basically empty) when you create a new Promise and to access the [[PromiseValue]] you would call the then with a function passed to the then method and somehow the [[PromiseValue]] will get passed to the parameter of the function you pass to .then? –  Feb 17 '17 at 17:50
  • probably yes because until we don't use handler function then or catch we can't get PromiseValue – Ashish Patel Feb 20 '17 at 06:40