Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

124
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda