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 "".

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 "".