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)
}