From 3b160ef2e13a9226738eca64f01c0fcfc19fb816 Mon Sep 17 00:00:00 2001 From: b Date: Sat, 19 Jun 2021 00:51:07 -0700 Subject: [PATCH] Hopefully fix async race stuff (3) --- load.js | 37 ++++++++++++++++++++----------------- load3.js | 38 ++++++++++++++++++++------------------ 2 files changed, 40 insertions(+), 35 deletions(-) diff --git a/load.js b/load.js index 5da7e93..23b8dad 100644 --- a/load.js +++ b/load.js @@ -137,26 +137,29 @@ function load_init(init_func) { console.log("DB failed to open..."); }; - request.onsuccess = async function() { - db = request.result; - if (!reload) { - console.log("Using stored data...") - load_local(init_func); - } - else { - if (load_in_progress) { - while (!load_complete) { - await sleep(100); - } - init_func(); + request.onsuccess = function() { + (async function() { + db = request.result; + if (!reload) { + console.log("Using stored data...") + load_local(init_func); } else { - // Not 100% safe... whatever! - load_in_progress = true - load(init_func); - console.log("Using new data...") + if (load_in_progress) { + while (!load_complete) { + await sleep(100); + } + console.log("Skipping load...") + init_func(); + } + else { + // Not 100% safe... whatever! + load_in_progress = true + console.log("Using new data...") + load(init_func); + } } - } + })() } request.onupgradeneeded = function(e) { diff --git a/load3.js b/load3.js index 269b04f..e3b8ca1 100644 --- a/load3.js +++ b/load3.js @@ -137,27 +137,29 @@ function load_init(init_func) { console.log("DB failed to open..."); }; - request.onsuccess = async function() { - db = request.result; - if (!reload) { - console.log("Using stored data...") - load_local(init_func); - } - else { - if (load_in_progress) { - while (!load_complete) { - await sleep(100); - } - console.log("Skipping load...") - init_func(); + request.onsuccess = function() { + (async function() { + db = request.result; + if (!reload) { + console.log("Using stored data...") + load_local(init_func); } else { - // Not 100% safe... whatever! - load_in_progress = true - console.log("Using new data...") - load(init_func); + if (load_in_progress) { + while (!load_complete) { + await sleep(100); + } + console.log("Skipping load...") + init_func(); + } + else { + // Not 100% safe... whatever! + load_in_progress = true + console.log("Using new data...") + load(init_func); + } } - } + })() } request.onupgradeneeded = function(e) {