Home > Back-end >  how to use async/await inside promise
how to use async/await inside promise

Time:07-09

I want to use async/await inside this promise.allSettled to convert currency by fetching some api or databased stored currency rates.

like i want to use await here like this, but not sure where to put async.

arrcars["price_client"] = await forexService.convertCurrency(item.regular_price, "EUR", userCurrency);

I have posted my entire code. need some guidance here.

This is controller

    const httpStatus = require('http-status');
    const pick = require('../utils/pick');
    const catchAsync = require('../utils/catchAsync');
    const async = require('async');
    const axios = require('axios');
    const { talixoService } = require('../services');
    const { iwayService } = require('../services');
    const { forexService } = require('../services');
    
        const searchCars = function (req, res) {
            const talixoData = talixoService.findTalixoCars(req.body) //call talixo api in services
            const iwayData = iwayService.findiWayCars(req.body) //call iwaytransfers api in services
        
            if (req.query.usercurrency) {
                var userCurrency = req.query.usercurrency;
            } else {
                var userCurrency = "INR";
            }
        
            return Promise.allSettled([talixoData, iwayData]).then(([restalixo, resiway]) => {
                const taxiresults = [];
                if (restalixo.status === "fulfilled") {
                    //console.log(restalixo.value.data);
                    if (restalixo.value.data.taxis.length) {
                        restalixo.value.data.taxis.forEach(function (item) {
                            arrcars = {};
                            arrcars["carname"] = item.car_model;
                            arrcars["originprice"] = item.regular_price;
                            arrcars["price_client"] = forexService.convertCurrency(item.regular_price, "EUR", userCurrency);
                            arrcars["discountprice_client"] = forexService.convertCurrency(item.discount_price, "EUR", userCurrency);
                            arrcars["packageId"] = item.id;
                            arrcars["image"] = item.image_url;
                            arrcars["freewaittime"] = item.included_waiting_time;
                            arrcars["maxluggage"] = item.luggage;
                            arrcars["maxpassengers"] = item.seats;
                            arrcars["discountprice"] = item.discount_price;
                            arrcars["vehicletype"] = item.vehicle_type;
                            arrcars["vendor"] = "Talixo";
                            arrcars["vehicleremarks"] = "Or Similar";
                            taxiresults.push(arrcars);
                        });
                    }
                    if (restalixo.value.data.limousines.length) {
                        restalixo.value.data.limousines.forEach(function (item) {
                            arrcars = {};
                            arrcars["carname"] = item.car_model;
                            arrcars["originprice"] = item.regular_price;
                            arrcars["price_client"] = forexService.convertCurrency(item.regular_price, "EUR", userCurrency);
                            arrcars["discountprice_client"] = forexService.convertCurrency(item.discount_price, "EUR", userCurrency);
                            arrcars["packageId"] = item.id;
                            arrcars["image"] = item.image_url;
                            arrcars["freewaittime"] = item.included_waiting_time;
                            arrcars["maxluggage"] = item.luggage;
                            arrcars["maxpassengers"] = item.seats;
                            arrcars["discountprice"] = item.discount_price;
                            arrcars["vehicletype"] = item.vehicle_type;
                            arrcars["vendor"] = "Talixo";
                            arrcars["vehicleremarks"] = "Or Similar";
                            taxiresults.push(arrcars);
                        });
                    }
                }
                //iwaytransfers supplier data
                if (resiway.status === "fulfilled") {
                    //console.log(resiway.value.data);
                    if (resiway.value.data.result.length) {
                        resiway.value.data.result.forEach(function (item) {
                            var imgsrc = "https://iwayex.com/images/cars/";
                            arrcars = {};
                            arrcars["carname"] = item.car_class.models[0];
                            arrcars["originprice"] = item.price;
                            arrcars["price_client"] = forexService.convertCurrency(item.price, "EUR", userCurrency);
                            arrcars["discountprice_client"] = forexService.convertCurrency(item.price, "EUR", userCurrency);
                            arrcars["packageId"] = item.price_uid;
                            arrcars["image"] = imgsrc   item.car_class.photo;
                            arrcars["freewaittime"] = item.allowable_time;
                            arrcars["maxluggage"] = "";
                            arrcars["maxpassengers"] = item.car_class.capacity;
                            arrcars["discountprice"] = item.price;
                            arrcars["vehicletype"] = item.vehicle_type;
                            arrcars["vendor"] = "iway Transfers";
                            arrcars["vehicleremarks"] = "Or Similar";
                            taxiresults.push(arrcars);
                        });
                    }
                }
        
                if (taxiresults.length) {
                    sortedresult = taxiresults.sort(function (a, b) {
                        return a.discountprice - b.discountprice;
                    });
                    res.status(200).send(sortedresult)
                }else{
                    res.status(200).send({})
                }
            });
        }

module.exports = {
    searchCars
}

this is the result sample i am getting.

{
carname: "Toyota Prius"
discountprice: 27.5
discountprice_client: {}
freewaittime: 45
image: "https://static.talixo.de/images/vehicles/economy.png"
maxluggage: 3
maxpassengers: 3
originprice: 27.5
packageId: "16021"
price_client: {}
vehicleremarks: "Or Similar"
vehicletype: "limo"
vendor: "Talixo"
}

here the problem is, i expect

discountprice_client: Converted Number, 

but i am getting blank object there as can be seen in above result sample. i have been using this in another function with async await, then it works fine. but in this case, i am not sure how to put async/await.

this is inside

forex.service.js

    const convertCurrency = async (amount, basecurrency, reqCurrency) => {
        if (basecurrency === reqCurrency) {
            let result = Math.round(amount)   ' '   reqCurrency;
            return result;
        } else {
            const reqForexData = await getForexbyISO(reqCurrency);
            const baseForexData = await getForexbyISO(basecurrency);
            const amountInUSD = amount / baseForexData.forexRate;
            const amountInreqC = amountInUSD * reqForexData.forexRate;
            let result = Math.round(amountInreqC)   ' '   reqCurrency;
            return result;
        }
    }

module.exports = {
    convertCurrency
}

CodePudding user response:

You could do:

// notice the `async` before `function`
resiway.value.data.result.forEach(async function (item) {
  // ...
  arrcars = {};
  arrcars["price_client"] = await forexService.convertCurrency(item.price, "EUR", userCurrency);
  arrcars["discountprice_client"] = await forexService.convertCurrency(item.price, "EUR", userCurrency);
  // ...
});

But it can lead to unexpected behaviours

I would do this

// add missing `async`
return Promise.allSettled([talixoData, iwayData]).then(async ([restalixo, resiway]) => {
  // ...
  // await for all loop at once to avoid odd side effects
  await Promise.all(resiway.value.data.result.map(async function (item) {
    // ...
    arrcars = {};
    arrcars["price_client"] = await 
  forexService.convertCurrency(item.price, "EUR", userCurrency);
    arrcars["discountprice_client"] = await 
  forexService.convertCurrency(item.price, "EUR", userCurrency);
    // ...
  }));
  // ...
});
  • Related