I am building an application in NodeJS version 8.5 with Express middleware.
I am using the async module to loop over an array of objects. The async.each method iterates over each object and processes each element.
Each object will be processed in a different way depending upon the property.
The way I have it now is to just call a bunch of synchronous methods inside the body of the async.each loop. However, I know that this isn't the 'proper' Node way since everything supposed to be asynchronous.
How can I structure this to use asynchronous methods properly?
async.each(elements_all, (element_original, callback) => {
// Create new array
const elements_processed = [];
// If element has order number property
if (element_original.order_number) {
element_original.order_number = processToDisplay(element_original.order_number); //This is a synchronous call
}
// If element has po number property
if (element_original.po_number) {
element_original.po_number = processToDisplay(element_original.po_number);
}
// If element has item price property
if (element_original.item_price) {
element_original.item_price = processToDisplay(element_original.item_price);
}
// Push to the return array
entities_processed.push(element_original);
callback();
}, (err) => {
if (err) {
res.json({
server_error: true
});
} else {
res.json(entities_processed);
}
});
processToDisplaythe async part?