Утилитка поможет писать красивый асинхронный код
Async.js
export class Async {
static async forEach(array = [], callback = (item, index) => Promise.resolve()) {
for (let i = 0; i < array.length; i++) {
await callback(array[i], i);
}
}
static async map(array = [], callback = (item, index) => Promise.resolve()) {
const out = [];
await Async.forEach(array, async (item, index) =>
out.push(await callback(item, index)));
return out;
}
}
main.js
import {Async} from './Async.js';
$('button[get_data]').click(async (e) => {
let date_begin = $('#date_before').val();
let date_end = $('#date_after').val();
/*if(date_begin == '' || date_end == '' || date_begin == undefined || date_end == undefined) {
alert('Выберите диапозон дат')
return false
}*/
/*$(e.currentTarget).prop('disabled', 'disabled')*/
let users = $('.chooses_users select').val();
$('.print_data .wrapper-table table tbody').html('');
await Async.forEach(users, async user => {
let user_id = user.split('|')[0];
let user_name = user.split('|')[1];
const res = await get_data_statistic(user_id, date_begin, date_end);
console.log('true');
});
console.log('false');
});