I have this code:
let channels = ["freecodecamp","test_channel","ESL_SC2"];
channels.map(function(channel) {
function genUrl(type, name) {
return 'https://wind-bow.gomix.me/twitch-api/' + type + '/' + name + '?callback=?';
}
$.ajax({ // 2 ajax requests
url: genUrl("streams", channel),
dataType: 'jsonp',
success: function(data) {
console.log("data = ",data)
setState(data);
$.ajax({
url: genUrl("channels", channel),
dataType: 'jsonp',
success: function(res) { // res = result
console.log("res = ",res)
setState(res);
},
});
},
});
});
but when I log, the resulting ajax isn't executing synchronously. on the map, I am expecting both ajax req to execute the same channel upon req(same index), but it's not, because the result is scrambled.
let's say on the first ajax, the map is executed like ["ESL_SC2","freecodecamp","test_channel"]
and on the second:
["freecodecamp","test_channel","ESL_SC2"]
I want the result to be same index as it was declared?
let channels = ["freecodecamp","test_channel","ESL_SC2"];
sample of my resulted logs:
data = {mature: false, status: "RERUN: Zest vs. data = {mature: false, status: "RERUN: Zest vs. Trust [PvP] - Group B Elimination Map 2 - IEM Gyeonggi 2016", broadcaster_language: "en", display_name: "ESL_SC2", game: "StarCraft II", …}background: nullbanner: nullbroadcaster_language: "en"created_at: "2012-05-02T09:59:20Z"delay: nulldisplay_name: "ESL_SC2"followers: 192328game: "StarCraft II"language: "en"logo: "https://static-cdn.jtvnw.net/jtv_user_pictures/esl_sc2-profile_image-d6db9488cec97125-300x300.jpeg"mature: falsename: "esl_sc2"partner: trueprofile_banner: "https://static-cdn.jtvnw.net/jtv_user_pictures/esl_sc2-profile_banner-f8295b33d1846e75-480.jpeg"profile_banner_background_color: "#050506"status: "RERUN: Zest vs. Trust [PvP] - Group B Elimination Map 2 - IEM Gyeonggi 2016"updated_at: "2017-09-05T18:05:46Z"url: "https://www.twitch.tv/esl_sc2"video_banner: "https://static-cdn.jtvnw.net/jtv_user_pictures/esl_sc2-channel_offline_image-5a8657f8393c9d85-1920x1080.jpeg"views: 68095345_id: 30220059_links: {self: "https://api.twitch.tv/kraken/channels/esl_sc2", follows: "https://api.twitch.tv/kraken/channels/esl_sc2/follows", commercial: "https://api.twitch.tv/kraken/channels/esl_sc2/commercial", stream_key: "https://api.twitch.tv/kraken/channels/esl_sc2/stream_key", chat: "https://api.twitch.tv/kraken/chat/esl_sc2", …}__proto__: Object
data = {mature: false, status: "TESTING TESTING TESTING", broadcaster_language: null, display_name: "test_channel", game: null, …}
data = {mature: false, status: "Some GoLang Today #go #golang #youtube", broadcaster_language: "en", display_name: "FreeCodeCamp", game: "Creative", …}
res = {stream: null, _links: {…}}
res = {stream: {…}, _links: {…}}
res = {stream: null, _links: {…}}
what I am trying to say here is like, on the first index of data let's say it's a result by "freecodecamp" as argument but on the 1st index res it's not "freecodecamp" being evaluated, it's "test_channel", so upon the execution of the map, I assume the response is long so it evaluates the next index? so in other words 1st index of data and 1st index of res is not match