駅データ.jp(API)の情報を取得した後に処理を実行したいのですが、
取得する前に実行されてしまうので、取得を待ちたいです。
javascript
1 let xml = {}; 2 let group_data = []; 3 function getGroup(code){ 4 let s = document.getElementsByTagName("head")[0].appendChild(document.createElement("script")); 5 s.type = "text/javascript"; 6 s.charset = "utf-8"; 7 s.src = `http://www.ekidata.jp/api/g/${code}.json`; //駅グループJSONデータURL 8 // s.src = "http://www.ekidata.jp/api/g/1130224.json"; //サンプルコード 9 let str = ""; 10 xml.onload = function(data){ 11 let station_g = data["station_g"]; 12 if(station_g != null){ 13 for( i=0; i< station_g.length; i++){ 14 group_data.push({'code':station_g[i].line_cd,'name':station_g[i].line_name}); 15 } 16 } 17 } 18 } 19 $(function(){ 20 $('body').on('click','.searchTrigger',(e) => { 21 const target = $('#ekiInput').val(); 22 const code = data[target]; 23 getGroup(code); 24 console.log(group_data) 25 }); 26 });
現在
console.log(group_data)
をすると空の配列が出力されてしまう。
xml.onload を待ちたいのですが、
うまくいきません。。。
async/awaitを使ってなんとかすることは可能なのでしょうか。。。