2017-12-30 53 views
1

非同期関数の内部から返されたデータをログに記録する方法はありますか?Javascript非同期関数コンソール返されたデータをログに記録

例: JSファイル:

async function getData(){ 
     try { 
     $.getJSON('./data.json', (data) => { 
      return data; 
     }); 
     } catch(error) { 
     console.log("error" + error); 
     } finally { 
     console.log('done'); 
     } 
    } 

    console.log(getData()); 

JSONファイル:

{ 
    "stuff": { 
     "First": { 
     "FirstA": { 
      "year": [2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017], 
      "Categories": ["Suspension", "Electrical", "Performance", "Motor"] 
     }, 
     "FirstB": { 
      "year": [2007, 2008, 2009, 2010, 2011, 2012], 
      "Categories": ["Suspension", "Electrical", "Performance", "Motor"] 
     } 
     }, 
     "Second": { 
     "SecondA": { 
      "year": [2002, 2003, 2004, 2005, 2006], 
      "Categories": ["Suspension", "Electrical", "Performance", "Motor"] 
     }, 
     "SecondB": { 
      "year": [2007, 2008, 2009, 2010, 2011, 2012], 
      "Categories": ["Suspension", "Electrical", "Performance", "Motor"] 
     } 
     } 
    } 
} 

はどのようにすれば/ JSONファイル内のすべての情報へのアクセスを取得し、それに取り組む返すことができます。例えば、 "First"と "Second"を取り出してdivに追加したいと思います。 "FirstA"、 "FirstB"、 "SecondA"、 "SecondB" ...と同様です。 {:}未定義

任意の助けをいただければ幸いです

は今のところ、そのまま、私は約束を取得します。

--------- --------- UPDATE

私は関数内でコンソールログを実行すると、私はその後、JSONデータを見ることができますが、私はへのアクセスを必要とします関数外のデータ。

セルジュ

+0

可能な重複https://stackoverflow.com/questions/14220321/how-do-:だから、それに.stuff、例えばを使用したいですi-return-as-asynchronous-call) – Andreas

答えて

関連する問題