0

I am returning a Promise in getInfo().

function getInfo() {
    return axios.get(url)
    .then((response) => {
        return response.userName
    }
}

Assuming that request can take a few seconds, I can wait for it with await by using async/await.

async function test() {
   let userName = await getInfo();
   // waiting for userName in order to do other stuff below
   // other stuff
}

if I didn't use async/await though, how would this just be done with Promises? Would this be the valid way to do it?

   function test() {
       let userName;
       getInfo().then(data => {
              userName = data;
       })
       // other stuff that depends on userName value
   }
Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
RicardoAlvveroa
  • 226
  • 1
  • 8

0 Answers0