Given this code:
let promise =new Promise(function(resolve,reject){
console.log("uno");
resolve();
});
promise.then(function(){
var cont=1;
promise.then(function(){
for (var i = 0; i < 100; i++) {
cont++;
};
console.log(5);
});
promise.then(function(){
console.log("dos");
console.log("tres");
});
});
promise.then(function(){
console.log("cuatro");
})
The order of execution I expect is:
- "uno" (let the for be executed)
- "five"
- "two and three"
- "four"
Note: I did test my code and it runs:
uno->cuatro->cinco->dos->tres
I understand that before resolve() is sent, the execution time of the tasks before resolve is respected, for example:
var p = new Promise(function (resolve, reject) {
setTimeout(function () {
console.log('cinco');
resolve('cinco');
}, 5000);
})
After 5 seconds have passed, it proceeds to execute the then (and it is what I was looking for, here the order of execution is respected)
but in .then, if one of them lasts longer than the others, it is executed at the end, I already understand that from the explanations, but isn't there any way to respect a then after it finishes, proceed through the other then?
.then(function (res) {
// res es igual a 'cinco'
setTimeout(function () {
console.log("dos");
},2000);
return 'dos';
})
.then(function (res) {
// res es igual a 'dos'
console.log("tres");
return 'tres';
});
the return is "three"->"two"
modification: new example with settimeout modifications in .then
var promise = new Promise(function (resolve, reject) {
console.log('uno');
resolve('uno');
});
promise.then(function (res) {
// res es igual a 'uno'
var p = new Promise(function (resolve, reject) {
setTimeout(function () {
console.log('cinco');
resolve('cinco');
}, 5000);
})
// Este then SI esta encadenado
.then(function (res) {
// res es igual a 'cinco'
setTimeout(function () {
console.log("dos");
},2000);
return 'dos';
})
// Este then NO esta encadenado al then anterior
.then(function (res) {
// res es igual a 'dos'
console.log("tres");
return 'tres';
});
// Acá le estamos indicando que todos los `then` encadenados
// esperen a que se resuelva esta nueva promesa
return p;
})
// Este then SI esta encadenado al then anterior
.then(function (res) {
// res es igual a 'dos', porque 'tres' no esta encadenado
console.log('cuatro: ' + res);
});
does not respect the order of execution in the
.then(function (res) {
// res es igual a 'cinco'
setTimeout(function () {
console.log("dos");
},2000);
return 'dos';
})
// Mistake:
What each statement
then
does isponer en cola
the passed function.This would be your code, with some small modifications, so that the execution flow is better understood.
As you can see, the functions will be executed in the order that they were
puestas en cola
, firstf1, f2, f3
and when executedf2
, the functionsf4 y f5
arepuestas en cola
, that is, afterf3
.// Solution:
// Update(
sin eliminar una función
)// Update(
timeout
)Chaining promises (
Chaining promises
), that is, each onethen
receives the value returned by the previous one.Using this property, it can be solved like this:
Important : Doing is not the same
promise.then
as.then()
(that is, doingchaining
).// Update(
nueva pregunta
)Yes, using
chaining
, as I already demonstrated in the Update of thetimeout
To solve
dos
you should do the same as incinco
, you should create onepromise
to wait for the response of thetimeout
(asynchronous pseudo-event) and make the onereturn
of this new promise.The "problem" lies in the asynchrony of nodeJS: although in certain scenarios it is a great advantage, in this one it harms you since you are looking for a sequential execution of the functions.
Through the use of promises you can achieve this if you chain them one after another, but they are designed for just the opposite: promises are launched so as not to block the execution thread.
I don't know exactly what you need to do but you can get the execution order you want by simply chaining the promises with the function
then
:What you return inside the
then
previous function will be received as an argument of the next functionthen
that is chained.