0

I am trying to return stock after my getstock function finishes. My problem is that my res.send function sends the response before my getstock function finishes resulting in an empty array.

 stock(@Req() req: Request, @Res() res: Response, next: NextFunction) {
    let reply: Stock[] = []
    let parts = req.body.cxml.credentials[0].partslist[0]
    for (let i = 0; i < parts.part.length; i++) {
        this.getStock(JSON.stringify(parts.part[i].partnumber[0]).replace(/['"]+/g, '')).then(stock => {
            reply.push(stock)
        })

    }
    console.log(reply)
    res.send(reply).sendStatus(200)

}
comsma
  • 3
  • 4
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – derpirscher Jul 05 '22 at 21:00
  • Try not using `then` but `await` instead. – Evert Jul 06 '22 at 01:13

0 Answers0