When we run the following code, it wait's for 5 seconds and produces three lines as result, as per my understanding of async await it should show single line every 5 seconds thus ending in 15 seconds. Can some one please help me what I am not doing correctly. the node.js code is as below :
const records = [
{ "REFNO": 466999, "VEHICLE": "Toyota Yaris 1.5", "MODELYEAR": 2018 },
{ "REFNO": 477555, "VEHICLE": "Nissan Altima 3.0", "MODELYEAR": 2020 },
{ "REFNO": 467000, "VEHICLE": "Honda Civic 2.0", "MODELYEAR": 2019 }
]
records.forEach(async (record) => {
let vehicleName = await fnGetVehicleName(record);
console.log(`Reference No. : ${record.REFNO} is a ${vehicleName}`);
});
function fnGetVehicleName(xvehicle) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(xvehicle.VEHICLE);
}, 5 * 1000)
})
}