I have the following HTML structure and I would like to fill each block with an Ajax call but when I use async:false it doesn't work in Jquery 3x
<ul class="tray" id="hiphop"></ul>
<ul class="tray" id="rnb"></ul>
<ul class="tray" id="afro"></ul>
<ul class="tray" id="blues"></ul>
<ul class="tray" id="dancehall"></ul>
<ul class="tray" id="rumba"></ul>
<ul class="tray" id="samba"></ul>
<ul class="tray" id="trap"></ul>
<ul class="tray" id="rap"></ul>
What I want to perform is this:
- browse the HTML structure and retrieve all the IDs
- send a request to my server file for each ID and
- display the result obtained according to the ID of each block
Here is my initial JS code:
$('.tray').each(function(){
$id = $(this).attr('id');
$.ajax({
type: 'GET',
url: 'feed',
data: 'c='+$id,
cache: false,
// async: false,
success:function(datas){
$('#'+$id+'').html(datas);=
},
error:function (){
console.log('error ...');
}
});
});
but after reading about something called Promise, I tried this but to be honnest, I don't even understand what I'm doing
var $array = $.map($('ul.tray'), function(value, index) {
return [value];
});
var promises = [];
$array.forEach(function(item) {
// you can access item.id and item.title here
$id = $(this).attr('id');
promises.push(
$.ajax({
type: 'GET',
url: 'feed',
data: 'c='+$id,
cache: false,
})
);
});
Promise.all(promises).then(function(results) {
// all ajax calls done here, results is an array of responses
//console.log($results);
});
Can you please help me to fill those empty blocks with Ajax without using async? Any help will be appreciated