admin管理员组文章数量:1401461
I am trying to use the variable bida
elsewhere in my code. For example, when I try to create the variable mida
. Is there a way to do that without nesting everything inside the getFX()
function? I understand it's an async function issue but how can I get around it?
const fetch = require("node-fetch");
async function getFX() {
let bida;
try {
var req = await fetch('');
var response = await req.json()
bida = await response.USD.bid;
aska = await response.USD.ask;
console.log(bida, aska)
} catch (e) {
// handle error
console.error(e)
}
}
getFX()
var mida = bida + 1;
console.log (mida);
I am trying to use the variable bida
elsewhere in my code. For example, when I try to create the variable mida
. Is there a way to do that without nesting everything inside the getFX()
function? I understand it's an async function issue but how can I get around it?
const fetch = require("node-fetch");
async function getFX() {
let bida;
try {
var req = await fetch('https://economia.awesomeapi..br/all/USD-BRL');
var response = await req.json()
bida = await response.USD.bid;
aska = await response.USD.ask;
console.log(bida, aska)
} catch (e) {
// handle error
console.error(e)
}
}
getFX()
var mida = bida + 1;
console.log (mida);
Share
Improve this question
edited Jun 20, 2020 at 16:35
Dan Knights
8,4084 gold badges27 silver badges54 bronze badges
asked Jun 20, 2020 at 15:27
MarceloFlaMarceloFla
231 silver badge3 bronze badges
3
-
Only if you
await getFX()
or use it in agetFX().then(...)
. Otherwise, no. That's the purpose of async functions, other code can run while the async task is being pleted. So withoutawaiting
it,bida
will beundefined
– blex Commented Jun 20, 2020 at 15:35 - what do you mean by without nesting? you can use a callback or you have to return a promise and await for getFX() – Zohaib Sohail Commented Jun 20, 2020 at 15:35
- Does this answer your question? Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference – ponury-kostek Commented Sep 14, 2020 at 18:27
1 Answer
Reset to default 7you should initialize the bida variable outside of the getFX() function to be able to access it elsewhere in that file, and also set the value of mira variable only when getFX resolves using .then() to be able to use the value of bida because getFX is an async function. Here is how I did it:
const fetch = require("node-fetch");
let bida;
async function getFX() {
try {
var req = await fetch('https://economia.awesomeapi..br/all/USD-BRL');
var response = await req.json()
bida = await response.USD.bid;
aska = await response.USD.ask;
console.log(bida, aska)
} catch (e) {
// handle error
console.error(e)
}
}
getFX().then(() => {
var mida = bida + 1;
console.log (mida);
})
Hope it helps !
本文标签: javascriptHow can I use a variable outside an async function in NodejsStack Overflow
版权声明:本文标题:javascript - How can I use a variable outside an async function in Node.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744298927a2599482.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论