ishaan
ishaan

Reputation: 59

Unable to return value from async method in typescript

I am working on typescript.I am trying to return a value from the async method.However , it always returns {}. Below is what I coded.

    async returnNewUrlAfterClickingLink(): Promise<string> {
    const allHandles = await browser.getAllWindowHandles();
    await browser.switchTo().window(allHandles[2]);
    const cur_url = await browser.getCurrentUrl();
    console.log("url is" + cur_url);
    return cur_url;
}

However I am able to log the url correctly.

Upvotes: 0

Views: 544

Answers (1)

Xotabu4
Xotabu4

Reputation: 3091

Your returnNewUrlAfterClickingLink is marked as async, so it always return promise now. In order to get this value, you need to await result of this function:

const url = await returnNewUrlAfterClickingLink()
console.log(url)

And again - to use await your function must be set async, so whole call stack must be async/await.

Hope this helps

Upvotes: 1

Related Questions