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

132
Vistas
Javascript is not returning objects as expected

Javascript (or Node.js) is not returning objects as expected.

The following code logs an empty array.

const _sodium = require('libsodium-wrappers');

const sodium = (async () => {
  await _sodium.ready;
  return _sodium;
})();

console.log(Object.getOwnPropertyNames(sodium));

On the other hand, the following code works as expected.

const _sodium = require('libsodium-wrappers');

(async () => {
  await _sodium.ready;
  const sodium = _sodium;
  console.log(Object.getOwnPropertyNames(sodium));
})();

In the first code snippet, it seems like the sodium object is tied to its lexical environment, which would explain why an empty array is printed. I would be interested in what's going on here (including an explanation of what's going on under the hood). Thanks.

almost 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

It's not about lexical context.

You just not awaiting for ready in the first example.

First example simplified:

(async() => {
  console.log('start')
  await new Promise(r => setTimeout(r, 3000))
  console.log('finish')
})()

Second example simplified:

console.log('start');
(async() => {
  await new Promise(r => setTimeout(r, 3000))
})();
console.log('finish')

almost 4 years ago · Santiago Trujillo 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