JSスニペット 非同期関連

複数非同期通信のスマートなawait

await並べるだけだと順次通信してしまうので工夫する
const get_schedules_days=async()=>{
	//これだと遅い
	//await get_a_schedules();
	//await get_b_schedules();
	let results=[];
	results.push(get_a_schedules());
	results.push(get_b_schedules());
    
	await Promise.all(results);


	schedules_days_show();
}
const get_a_schedules=async()=>{
	await $.ajax({
		type : "GET",
		url : "./rest/get_a_schedules/",
		data : {
		},
		dataType : "json",
		cache : false
	}).done(function (data) {
	}).fail(function (data) {
	}).always(function (data) {
	});
}
const get_b_schedules=async()=>{
	await $.ajax({
		type : "GET",
		url : "./rest/get_b_schedules/",
		data : {
		},
		dataType : "json",
		cache : false
	}).done(function (data) {
	}).fail(function (data) {
	}).always(function (data) {
	});
}

コメント

このブログの人気の投稿

ネットギア WiFi6 無線lan 法人向け メッシュ アクセスポイント WAX610の初期設定

Raspberry Pi 4 でデュアルディスプレイの拡張・複製

【更新】福祉施設向け事務用PC選定