我试图使用一个名为public-ip的包,但是当我在github上使用示例时,我不能使用异步代码之外的变量。
我尝试过各种解决方案,通过设置函数。
function GetAddress() {
return await publicIp.v4()
}但这些只是回报一个承诺,我已经尝试谷歌如何不得到一个承诺/得到它的IP,但没有任何运气。
任何帮助都是非常感谢的。
发布于 2020-07-18 12:03:38
定义一个异步函数并按如下方式使用它:
async function GetAddress () {
return await publicIp.v4();
}
GetAddress()
.then( address => {
console.log(address);
});发布于 2020-07-18 12:02:10
.then()可能会解决您的问题:
publicIp.v4()
.then(ip => {
// ip is the value, do your logic here
console.log(ip)
})
.catch(error => {
// if it throws an error, you can catch it and suppress it here
throw error;
});https://stackoverflow.com/questions/62968206
复制相似问题