0

I am trying to call some function using a single express router , I want to call them in order, meaning that I don't want getLaps() function to execute before get streams function has done all the work , so I tried to use some solutions I found on the internet but it didn't work, the second function doesn't execute. Please help.

Here is my code :

router.get("/", async (req, res,done) => {
        res.status(201).send('created user')
        return getLaps(function () {
                  getStreams(function () {
                 });
        });

       // await getStreams();
       // await getLaps();
       // console.log("hey")
    });

Here is the get laps function :

function getLaps(req) {

    const access_token = '75f2d92fdc445033312854d775e039b6c5bf04e7';


    //for test 3756582581,
    const idL = [5567017025, 5566531480];


    const stravaClient = StravaClientService.getClient(access_token);
    const activityService = StravaActivityService(stravaClient);

    var params = {
        TableName: "run-id",
        Key: {
            "id": "15428785",
        }

    };

    console.log("cool laps")

    docClient.get(params, async function (err, data) {
        if (err) {
            console.log("Error", err);

        } else {

        }

        idL.map((id, index) => setTimeout(() => activityService.listLaps(id), (5 + index) * 60)
        )

        //data.Item.json

    });
}

and the streams function :

function getStreams(req) {

    const idS = [
        5567017025, 5566531480

    ];

    const stravaClient = StravaClientService.getClient(access_token);
    const activityService = StravaActivityService(stravaClient);

    var params = {
        TableName: "run-id",
        Key: {
            "id": "15428785",
        }

    };
    console.log("cool streams")

    docClient.get(params, async function (err, data) {
        if (err) {
            console.log("Error", err);

        } else {

            idS.map((id, index) => setTimeout(() => activityService.streamActivity(id), (5 + index) * 60))
            console.log("got the streams")
          
        }
    });

}
Vidarshan Rathnayake
  • 484
  • 1
  • 10
  • 20
justLearning
  • 59
  • 1
  • 7
  • 1
    does `getLaps` take a callback to call? or is it `getStreams` that you're asking about? without seeing getLaps or getStreams ... who knows – Bravo Jul 23 '21 at 12:10
  • Please post the fns `getLaps` and `getStreams`! – netlemon Jul 23 '21 at 12:13
  • Calling `return` will exit the function. However that code is a complete mess. I have no idea what you're trying to do here but probably something roughly like this? https://pastebin.com/pFgJywJG –  Jul 23 '21 at 12:14
  • i edited and added the getstreams and getlaps functions – justLearning Jul 23 '21 at 12:22
  • What's up with those timeouts? Why don't your functions return anything? – Bergi Jul 25 '21 at 13:58

1 Answers1

-1

in your getStream and getLaps function return promises instead of other object/Stuff like

async function getStream(){
   return new Promise(async (resolve, reject){
    //Do something
    //where you want to return something just call resolve function like
   resolve()
   //if you want some output of getStream() just pass it to resolve function
   //const result = 'I'm result'
   resolve(result)
})
}

do same thing with the laps function and in your router call them with await keyword

Ali Faiz
  • 212
  • 2
  • 9