Skip to content

Instantly share code, notes, and snippets.

@humansonofhuman
Last active July 31, 2018 00:01
Show Gist options
  • Save humansonofhuman/0d7e560e480ffe24a5900d7f91567d2f to your computer and use it in GitHub Desktop.
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
//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
});
//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