Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

217
Vistas
Why doesn't the background color change first?

'use strict';

const sum = num => {
  let acc = 0;
  while (num) {
    acc = acc + num;
    num = num - 1;
  }
  return acc;
};

new Promise((res, reject) => {
  document.querySelector('body').style.backgroundColor = 'red';
  res();
}).then(() => {
  const res = sum(1000000000);
  console.log(res);
});

I want to change the bgcolor to red first and get result of the sum function.

But it's not working well.

After receiving the sum result, change the bg color.

what i missed?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I believe the code is working correctly. However, after setting the background color, you immediately lock up the processor with an extremely long loop. The browser does not have time to color the background before it gets frozen calculating the sum of 1 to a billion. While the property of the background color will be set before the function, the actual re-rendering takes a non-zero amount of time.

You can see this by setting a delay before letting the promise return. In this case (or at least on my processor anyway), the background will turn red first.

'use strict';

const sum = num => {
  let acc = 0;
  while (num) {
    acc = acc + num;
    num = num - 1;
  }
  return acc;
};

new Promise((res, reject) => {
  document.querySelector('body').style.backgroundColor = 'red';
  setTimeout(res, 250);
}).then(() => {
  const res = sum(1000000000);
  console.log(res);
});

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda