arreglos asincronos

by scyrizales

HTML

<div></div>

JavaScript

function imprimir(valor) {
	var div = document.querySelector('div');
  div.innerHTML += valor + '<br />';
}

var acumulador = 0;
var arreglo = [1,2,3,4,5];
var longitud = arreglo.length;
arreglo.forEach(function(e, ix) {
	setTimeout(function () {
  	acumulador = acumulador + e;
    if (ix === longitud - 1) {
    	imprimir(acumulador);
    }
  }, 1000);
});
imprimir(acumulador);