javascript获取价值而不是承诺(javascript Get value instead of promise)

编程入门 行业动态 更新时间:2024-10-10 19:16:54
javascript获取价值而不是承诺(javascript Get value instead of promise)

我希望从承诺中获得价值,但不知道如何。

我试过这种方式:

function connected(p) { var url = getURL(); async function getURL() { var test = ""; let tab = await browser.tabs.query({currentWindow: true, active: true}); tab.then(function(tabb){ test = tabb[0].url.toString() }); return test; } async function getURL() { var tab = await browser.tabs.query({currentWindow: true, active: true}); return tab[0].url; } console.log(url.toString()); // Promise }

第一个函数被拒绝,第二个函数被填满。

I would like get a value from a promise but dont know how.

I tried this way:

function connected(p) { var url = getURL(); async function getURL() { var test = ""; let tab = await browser.tabs.query({currentWindow: true, active: true}); tab.then(function(tabb){ test = tabb[0].url.toString() }); return test; } async function getURL() { var tab = await browser.tabs.query({currentWindow: true, active: true}); return tab[0].url; } console.log(url.toString()); // Promise }

The first function get rejected the second one is fullfilled.

最满意答案

我希望从承诺中获得价值,但不知道如何。

从promise中获取值的唯一方法是在promise或同一函数中使用.then() ,可以使用await 。

async函数始终返回一个promise。 在函数中,您可以使用await来“等待”获取值的promise,但这不是函数返回值的情况。 该函数总是返回一个promise,你总是使用await或.then()来获取promise的值。

所以,你的第二个getURL()函数返回一个promise,它的解析值是你想要的url。 要获得该值,请在返回的promise上使用.then() :

async function getURL() { var tab = await browser.tabs.query({currentWindow: true, active: true}); return tab[0].url; } getURL().then(url => { console.log(url); });

或者,在这里使用await确实没什么大优势所以你也可以这样做:

function getURL() { return browser.tabs.query({currentWindow: true, active: true}).then(tab => { return tab[0].url; }); } getURL().then(url => { console.log(url); });

您的第一个版本的getURL()不起作用,因为您的函数在调用.then()处理函数之前返回,因此您总是只返回"" 。

I would like get a value from a promise but dont know how.

The only way to get a value from a promise is by using .then() on the promise or within the same function, you can use await.

An async function always returns a promise. Within a function, you can use await to "wait" for a promise to get the value, but that is not the case for the return value for the function. The function always returns a promise and you always use either await or .then() to get the value from a promise.

So, your second getURL() function returns a promise who's resolved value is the url you want. To get that value, you use .then() on the returned promise:

async function getURL() { var tab = await browser.tabs.query({currentWindow: true, active: true}); return tab[0].url; } getURL().then(url => { console.log(url); });

Or, there's really no big advantage in using await here so you could also just do:

function getURL() { return browser.tabs.query({currentWindow: true, active: true}).then(tab => { return tab[0].url; }); } getURL().then(url => { console.log(url); });

Your first version of getURL() does not work because your function returns BEFORE your .then() handler is called and thus you always just return "".

更多推荐

本文发布于:2023-08-07 17:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465329.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   价值   javascript   promise

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!