Skip to content Skip to sidebar Skip to footer

Run Synchronouse Function In A Promise

I am new to JS and async operations. In a router of nodeJS using express, I have aggregated some data from mongo using mongoose. The data is weather data collected from different s

Solution 1:

I think I have figured it out:

  1. after all, have to make all synchronous functions asynchronous by prepending async to those functions;
  2. rewrite this part in the post router function, especially the array map part. I read from this. and in the map() gonna have try...catch... in it, otherwise it won't work.

    await stationDataCursor.toArray().then(async (docArr)=>{
    
            const newfeature = awaitPromise.all(docArr.map(asyncfunction(x){
                try{
                    const feature = await calcDSV.calcDSV(x);
    
                    return feature
                } catch(err){
                    console.log("Error happened!!! ", err);
    
                }
    
            }));
    
            res.json(newfeature)
    
    })
    

Hope it helps.

Post a Comment for "Run Synchronouse Function In A Promise"