最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I use a variable outside an async function in Node.js? - Stack Overflow

programmeradmin5浏览0评论

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 a getFX().then(...). Otherwise, no. That's the purpose of async functions, other code can run while the async task is being pleted. So without awaiting it, bida will be undefined – 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
Add a ment  | 

1 Answer 1

Reset to default 7

you 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 !

发布评论

评论列表(0)

  1. 暂无评论