0

I'm using $.getJSON to return a promise object. But I want to store the data before it goes through cleanData() in a variable called 'unclean'. I also want the data that goes through cleanData() to be stored in a variable called 'clean'. Right now this is not working-- when I do this (below) and log both unclean and clean they are the same (the data after cleanData())

var unclean, clean;

getData('data/allmetros.json')
  .then(function(data1) {
    unclean = data1;
    return cleanData(data1);
  }).done(function(data2) {
    clean = data2;
    console.log(unclean, clean);
});  

function getData(url) {
  return $.getJSON(url); 
}

function cleanData(data){
  //do something to clean the data
  return data;
}
NewToJS
  • 2,011
  • 4
  • 35
  • 62
  • In the example code you have provided the two variables will of course have the same contents, given that `cleanData` returns the input unaltered. Please [edit] your question to show your actual code if that is where the problem lies, and also include an example JSON that lets us reproduce the issue – Bergi Sep 27 '16 at 21:34
  • Protip: Never use `done`, always use `then` (although it doesn't matter in this case, it's a best practice) – Bergi Sep 27 '16 at 21:35
  • The duplicate does answer the question in your post title, and shows better approaches than two variables to store success results. If that's not your actual question, please [edit] and I'll reopen. – Bergi Sep 27 '16 at 21:38
  • it looks like your problem is due to saving data1 by reference. try duplicating the variable before – yuria Sep 27 '16 at 21:40

0 Answers0