Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

437
Views
Exceljs espera que no funcione en promesa en la función asíncrona

Actualmente estoy trabajando para abrir y escribir en un archivo de Excel usando exceljs. Sin embargo, la espera está cometiendo un error.

 SyntaxError: await is only valid in async functions and the top level bodies of modules

A pesar de que la función está en asíncrono. ¿Como puedo resolver este problema? El siguiente es el código que estoy usando

 async function Process(filename){ const workbook = new ExcelJS.Workbook(); let myPromise = new Promise(function(myResolve, myReject) { // "Producing Code" (May take some time) try{ await workbook.xlsx.readFile(filename) myResolve(); // when successful }catch(err){ myReject(); // when error } }); // "Consuming Code" (Must wait for a fulfilled Promise) myPromise.then( function() { /* code if successful */ }, function() {return false;} ); }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

De acuerdo con los comentarios, debe evitar el antipatrón del constructor Promise y nunca usar await en el ejecutor de una new Promise .

Deberías escribir cualquiera

 function Process(filename){ const workbook = new ExcelJS.Workbook(); // "Producing Code" (May take some time) let myPromise = workbook.xlsx.readFile(filename); // "Consuming Code" return myPromise.then(function() { /* code if successful, waiting for a fulfilled Promise */ return …; }, function() { /* code when error, waiting for a rejected Promise */ return false; }); }

o

 async function Process(filename){ const workbook = new ExcelJS.Workbook(); try { // "Producing Code" (May take some time) await workbook.xlsx.readFile(filename); // "Consuming Code" (wait for the promise to be fulfilled) return …; } catch(err) { // Code when error in producing or consuming code return false; } }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!