I am developing an API in javascript using node.js
What I'm trying to do is call a function which fires a chain of promises and on callback it function returns an array with information for one product.
What I want to do is to run this function as many times as required to form a list of products in JSON format.
But this list of products needs to be created before it's sent back to the user, which is where I'm struggling.
This is the function I have:
exports.findProduct = (ingredient, store, callback) => {
products.getProductData(ingredient, store)
.then( data => products.getProducts(data))
.then( productList => products.filterProductData(productList))
.then( selectedProduct => callback(null,selectedProduct))//get individual products and add to list
.catch( err => callback(err))
}
I call it like this
products.findProduct(ingredient, 'Waitrose', (err, data) => {
if (err) {
//res.send(status.badRequest, {error: err.message})
} else {
res.send(data)
res.end()
}
})
The res.send(data) sends the data from the callback back to the server, but what if I want to run this function again and update an array and only then send the array to the server?
Any help of tips would be very appreciated.