PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 24, 2022

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

 July 24, 2022     javascript, json, oop     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing