lundi 28 juin 2021

Using if else condition with promises and fetch [closed]

The code flow is as follows (javascript):

function getData(){
  return new Promise((resolve,reject)=>{
    fetch(`some_url`)
    .then(
      //some code
    )
    .then(
      //some code
      resolve(d)
    )
  })
}

Then the above function is called:

getData().then((data)=>{

  //some code that stores necessary values from the fetch api into variables

  if(condition){
    //check for condition and uses variables fetched from api
    //some code
  }
  else{
    //some code that uses variables fetched from api
  }
}))

The problem is that 'if' as well as 'else' both the blocks are executed. Can someone please help me with this

Aucun commentaire:

Enregistrer un commentaire