-3
var myids = '';

function passid(){
    onload = function () {
        uidt = tempid.ssid; // some value getting from API
    }
}

passid();
console.log(myids);

Can Anyone help me with this, How to push uitd to var myids so I can be use wherever I want.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • It's unclear what issue you're actually having. If `tempid.ssid` is coming from an async function, then see [How do I return the response from an aynchronous call](https://stackoverflow.com/q/14220321/438992). – Dave Newton Mar 27 '22 at 18:14

1 Answers1

0

Your API request is definitely asynchronous, so you can use the promises via async await if you are inside an asynchronous function or then and catche if you are outside an asynchronous function

var mydata = '';

async function apiReq(){
  
  return await fetch('https://jsonplaceholder.typicode.com/todos/1').then(response=>response.json()).then(response=>{
    mydata = response
    console.log("test");
    console.log(mydata);
  
  })
  


}


apiReq().then(response => {
  console.log("request is ok")
  console.log(mydata);

});
Mido elhawy
  • 469
  • 4
  • 11