I
I
ikerya2018-03-11 13:15:32
JavaScript
ikerya, 2018-03-11 13:15:32

Why does a promise call the next then if the previous one doesn't return anything?

Hello. Can you please tell me why the promise is called, which follows after the promise of the user.get function, if the latter does not return anything?

var user = {
  get: function() {
    return new Promise(function(resolve, reject) {
      setTimeout(resolve, 1000);
    });
  },
  set: function() {
    return new Promise(function(resolve, reject) {
      setTimeout(resolve, 2000);
    });
  }
};

user.get().then(function() {
  console.log(new Date(), "get");
}).then(function() {
  console.log(new Date(), "set");
});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
kn1ght_t, 2018-03-11
@ikerya

because that's how the chick's then call works:
it goes all the
way, and it doesn't matter if you return a promise (or whatever) or not
such a construction will first print "hello world", and then "undefined", because in the 2nd then we did not pass anything

M
mortyy, 2018-03-11
@mortyy

resolve is called on the first promise and that's enough.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question