0

I want to get the Current Tab Url at the start of execution of popup.js while the code execution does not move forward to the next lines, how to do that? This is my code :

    let problemIndex = "";
    let contestId = "";
    let pathName = "";
    let urlArr = [];
    // Finding the Url of the Current Page
    chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
        currentTab =(tabs[0].url);
        console.log(currentTab);//1
    });
    console.log(currentTab);//2
    //Finding ContestId && ProblemIndex
    pathName = JSON.stringify(currentTab);
    urlArr = pathName.split("/");
    console.log(pathName);//3
    if (urlArr[4] == "contest") {
        contestId = urlArr[1];
    } else {
        contestId = urlArr[urlArr.length - 2];
    }
     problemIndex = urlArr[urlArr.length - 1];

I want execution of console.log() to be like 1 then 2 then 3 But the execution goes like this 2 then 3 then 1 . How to change this ?

  • You'll have to put the subsequent code inside the callback because it's asynchronous. A more modern approach would be to use async/await syntax. – wOxxOm Aug 12 '21 at 13:59
  • Yeah i tried that but after this function I have more async functions and also in the end I have an event listner for a submit button , so how to handle it , do I put everything in the async await ? – Nikhar Manchanda Aug 12 '21 at 14:23
  • You can use as many `await` inside an `async` function as you want, there's no limit, it's a perfectly valid approach. – wOxxOm Aug 12 '21 at 14:37

0 Answers0