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

326
Vistas
What is the await keyword effect when importing ESM modules

Node.js docs' crypto module documentation uses top level await to import, but it's clearly not a dynamic import

source: https://nodejs.org/dist/latest-v15.x/docs/api/crypto.html#crypto_static_method_certificate_exportchallenge_spkac_encoding

What is the difference between the 2 statement below (they seems to do the same)

import * as fs from 'fs'
const fs_ = await import('fs');        // why use this?
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

[Not Duplicate]

  • The await import() imports an EC Module statically (instead of import() imports dynamically), it's very similar to the CommonJS module require() syntax, this is the only purpose of the top level await keyword

Supported by: (node.js 14.8.0) (chrome 89) (Firefox 89) at the time of writing this

  • So why using it if we already have all those beautiful static import syntaxes? (like: import * as imp from './someModule.mjs)
  • Well it's more straightforward to write and easier to remember (ex: await import('./someModule.mjs') statically requires the 'someModule' and returns its exported data as {default:'someDefVal'[, ...]} object
// static import -----------------------------------// blocks this module untill './module1.mjs' is fully loaded 
  // traditional static import in EC modules 
    import * as imp from './module1.mjs'; 
    console.log( imp );                             // -> {default:'data from module-1'}
    
  // static import with await import() (does the same as above but is prettier) 
    console.log( await import('./module1.mjs') );   // -> {default:'data from module-1'}
    
    function importFn1(){                           // the await import() is a satic import so cannot be used in a function body
        // await import('./module1.mjs');           // this would throw a SyntaxError: Unexpected reserved word 
    }
    
    
// dynamic import ----------------------------------// does not block this module, the './module2.mjs' loads once this module is fully loaded (not asynchronous)
    import('./module2.mjs') 
        .then((res)=>{ console.log(res) })          // -> {default:'data from module-2'}
        .catch((err)=>{ console.log(err) });
        
    (function ImporFn2(){
        import('./module2.mjs')                     // dynamic import inside a function body 
            .then((res)=>{ console.log(res) })      // -> {default:'data from module-2'}
            .catch((err)=>{ console.log(err) });
    })();
    
    console.log( '----- MAIN MODULE END -----' );
  • result in console
{ default: 'data from module-1' }
{ default: 'data from module-1' }
----- MAIN MODULE END -----
{ default: 'data from module-2' }
{ default: 'data from module-2' }
over 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