Last active
July 31, 2018 00:01
-
-
Save humansonofhuman/0d7e560e480ffe24a5900d7f91567d2f to your computer and use it in GitHub Desktop.
Solución a problema de funciones asíncronas dentro de un foreach que quería se ejecutaran de forma sincrona
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Son los contenidos | |
var contenidos = [ | |
3000, | |
1000, | |
2000 | |
]; | |
contenidos.forEach(element => { | |
setTimeout(()=>{ | |
//Es la ejecucion del sql en la bdd | |
console.log(element); | |
}, element); | |
//Hay que hacer que espere a que se termine | |
//De ejecutar este codigo antes de pasar al | |
//Siguiente elemento del array | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Son los contenidos | |
var contenidos = [ | |
3000, | |
1000, | |
2000 | |
]; | |
function procesarContenidos(err, conts, cb) { | |
if(err) return console.log(err); | |
var temp = conts.shift() | |
setTimeout(()=>{ | |
//Es la ejecucion del sql en la bdd | |
console.log(temp); | |
if(conts.length < 1){ | |
return console.log("listo"); | |
}else{ | |
procesarContenidos(null, conts, cb); | |
} | |
}, temp); | |
}; | |
procesarContenidos(null, contenidos, ()=>{ | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment