Sunday, July 24, 2022

[FIXED] How can i use JSON data out of callback function

Issue

Hi I'm trying to get JSON data from a JSON file. I got it on my JS code but i cant use it out of the callback function. I want to use the data anywhere in my code. I want to pass the data to a variable

My code for getting JSON data is :

async function load() {
    let url = 'sorular.json';
    let obj;
    obj = await (await fetch(url)).json();
    return obj
}
    load().then(res=>{console.log(res)})

Solution

in the THEN, simply call a separate function where res is passed as argument.

function processJSON(data){
  //run your other functions here
}

load().then(res=>{processJSON(res)})


Answered By - imvain2
Answer Checked By - Robin (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.