¡GoConqr estrena diseño! Cuéntanos qué te parece.

Módulo 11 de 23 · Multimedia

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. 

Instrucciones

Para avanzar en el curso, envía un intento de este test. Puedes enviarlo después de la última pregunta.

Ir al módulo elegido

Escríbenos

¿Sobre qué es tu mensaje?

¿Qué te parece el nuevo diseño?

¿Te gusta?