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

96
Views
¿Cómo uso $.when() en lugar de esperar en la siguiente función?

Tengo las siguientes 2 funciones:

 $(document).on(`dragover drop change`, `.fileUpload`, async function(e) { e.stopPropagation(); e.preventDefault(); const thisEl = $(this); if (thisEl.val() === `` || e.type === `dragover`) { return; } const headerRec = await getCSVHeaderRow(thisEl[ 0 ].files[ 0 ], `,`); console.log(`header rec: `, headerRec,headerRec2); }); function getCSVHeaderRow(file, delim) { return new Promise((resolve, reject) => { let reader = new FileReader(); reader.onload = function(e) { let headerRow = []; const rows = e.target.result.split(`\r\n`); headerRow = rows[ 0 ]; console.log(`headerrow: `, headerRow, headerRow.split(delim)); resolve(headerRow.split(delim)); }; reader.onerror = reject; reader.readAsText(file); }); }

Estoy tratando de usar $.when() en lugar de esperar, pero la función llamada devuelve un objeto de promesa en lugar de la matriz:

 $(document).on(`dragover drop change`, `.fileUpload`, function(e) { e.stopPropagation(); e.preventDefault(); const thisEl = $(this); if (thisEl.val() === `` || e.type === `dragover`) { return; } const headerRec2 = $.when(getCSVHeaderRow2(thisEl[ 0 ].files[ 0 ], `,`)).then(function(data) { return data; }); console.log(`header rec: `, headerRec,headerRec2); }); function getCSVHeaderRow2(file, delim) { const dfr = $.Deferred(); let reader = new FileReader(); reader.onload=function(e) { let headerRow = []; const rows = e.target.result.split(`\r\n`); headerRow = rows[ 0 ]; console.log(`headerrow: `, headerRow, headerRow.split(delim)); return headerRow.split(delim); }; return dfr.resolve(reader.readAsText(file)); }

¿Qué debo cambiar en getCSVHeaderRow2() para que devuelva la matriz de la forma en que lo es getCSVHeaderRow()?

about 4 years ago · Juan Pablo Isaza
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!