GoConqr has a new look! Let us know what you think.

Module 11 of 23 · Media

The .then() function

Kostas Diakogiannis

The .then() function

Chaining promises - the .then function

What about this syntax advantage that promises offer against nested callbacks then? 

What if we want something to happen AFTER the promise has been fulfilled and the new ruler is indeed danny? 

Well in order to chain promises as we call it, you need to use the .then() function as seen above. The line 17 executes the changeRuler function, which sets Danny as a ruler after 3 seconds and returns a promise. If we want to do something after this promise, we use the .then and inside we pass as an argument another function. 

This function (anonymous) will define what is going to be done directly after the previous promise was resolved. If we need to have access to data from the previous function, we can! The inner function accepts an argument that represents exactly that! The data that has been passed from the previous function inside the resolve! In our case the ruler's name.

In fact, resolve acts like a return statement for promises. Not only defines that a promise's state is fulfilled, but also which data will 'travel' into the next step of this journey (the next .then() function in this case).

In our case the data argument corresponds to the value of the ruler variable that was passed into the previous resolve. 

Instructions

You must submit an attempt for this quiz to progress. You can submit it after the last question.

Go to the chosen module

Feedback

What is it about?

What do you think of the new look?

How do you like it?