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

129
Vistas
Complete function only after stream finish

I am using PDFKit for generating invoice PDFs, just came up with an issue, where my function is returning undefined, but PDF is getting generated (maybe a bit late). I want my function to only return once all work Is done, from generating PDF to completing the file.

My function is like this:

exports.generatePdf = async (invoiceId) => {
  try {

    let invoicePath = `uploads/invoices/filename.pdf`

    // Create doc
    let doc = new PDFDocument({ size: "A4", margin: 30 })
    const stream = fs.createWriteStream(invoicePath)
    doc.pipe(stream)

    // All doc styling goes here

    // End
    doc.end()

    stream.on('finish', function () {

      return invoicePath

    })

  } catch (err) {
    console.error(err)
    throw err
  }
}

I want my function (generatePdf) to return invoicePath after everything is completed.

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