3

I understand that a new promise has a .then method on it.

But when I console log the promise returned from getUserData('jeresig') I get this object logged to the console (with no .then method).

Promise {
  _bitField: 0,
  _fulfillmentHandler0: undefined,
  _rejectionHandler0: undefined,
  _promise0: undefined,
  _receiver0: undefined }

Is .then actually on the new promise object or perhaps it only gets added to the object later asynchronously?

So does that mean .then is called asynchronously even though it looks synchronous?

let Promise = require('bluebird'),
    GitHubApi = require('github'),

let github = new GitHubApi({
  version: '3.0.0'
});

function getUserData (user){
    return new Promise(function(resolve, reject){
      github.user.getFollowingFromUser({
        user: user,
        per_page: 2
      }, function(err, res){
        if (err){ reject(err) }
        else {
          resolve(res)
        }
      })
    })
}

console.log(getUserData('jeresig'))

  // .then(function(data){
  //   console.log('data', data)
  // })
  // .catch(function(error){
  //   console.log('error', error)
  // })
Bergi
  • 630,263
  • 148
  • 957
  • 1,375

3 Answers3

1

The .then() handler is already there. This is just an issue with what you're looking at in the console.

It's on the prototype of the object which is not shown by default in console.log(). Since it looks like you are looking at a Bluebird promise, if you look at the returned promise in the console in Chrome, you can expand the object in the console and see everything that is on the promise object, including the methods on the prototype. .then() is there from the moment the promise is created.

You can also see for sure that it's there by doing this:

var p = getUserData('jeresig');
console.log(p.then);

And, .then() has to be there initially or the whole concept of the promise will not work because you call .then() on it synchronously. That's how you register your interest in knowing when the promise is resolved in the future.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

Yes, .then is actually on the new promise object after creation (it in Promise prototype). And it is synchronous, because it still add callbacks to fulfill and reject events.

Try

console.log(getUserData('jeresig').then)
Dima Fitiskin
  • 323
  • 1
  • 6
0

So does that mean .then is called asynchronously even though it looks synchronous?

then method is synonymous with "do something after this promise is resolved". then behaves differently according to the Promise State (Polymorphism) which can confuse initially unless you read the source or spent some time understanding the concept.

If Promise1 is resolved already, then it returns a Promise2 to you and also settles that Promise2 with the transformation function supplied by then. If Promise1 is not resolved at that point, it registers a function that would get called after Promise1 is resolved and returns Promise2. In either case you immediately get a Promise2 object. callbacks doesn't return anything to you which is not really functional programming, it just relies on after effect.

The main idea of Promise vs the normal callback is that it returns an Object to you immediately which is called the Promise Object. The advantage is that you can immdeiately attach a future event to it by using the then when this Promise say Promise1 is completed and not somewhere else in the program. This is highly scalable say if you want to loop and attach something then.then.then etc!

This return Object is an important concept of Promise, if you didn't get this Object in return you would have to do this in the callback1 function somewhere else in the program and not instantly. Imagine having to register one more aysnchronous call after callback1. It starts to get really messy. Everything works, however it will start getting complex to maintain. And you are not going to write functional programming like this https://stackoverflow.com/a/35786369/452102 which is really an idiomatic way to write things.

Going through an implementation of Promise will be more beneficial to learn the concepts since there are lot of places where API behaves differently depending on the situation. The benefit of this Promise abstraction is that, once the end user gets used to it, it all looks very seamless.

Community
  • 1
  • 1
Nishant
  • 20,354
  • 18
  • 69
  • 101