使用dexie db时以意外顺序调用Javascript函数
我在加载一个调用其他四个函数的页面时执行了一个函数:使用dexie db时以意外顺序调用Javascript函数,javascript,dexie,Javascript,Dexie,我在加载一个调用其他四个函数的页面时执行了一个函数: // Initialise DB await initialiseDb (); // Synchronise server's and local db await synchroniseClientAndServer (); // Use the languages from the local db addLanguagesToSelection (); // Star
// Initialise DB
await initialiseDb ();
// Synchronise server's and local db
await synchroniseClientAndServer ();
// Use the languages from the local db
addLanguagesToSelection ();
// Start synchronisation loop
synchroniseClientAndServerLoop ();
1-dexie db的初始化
2-同步来自服务器的数据,将数据保存在本地表上
3-使用本地表上保存的数据
4-循环,每隔5秒执行#2
由于某些原因,它们按顺序执行#1#3#2和#4
因此,这些语言不会出现,因为列表是在加载数据之前填充的。
我知道这个问题可能是因为dexie db是异步的,但我几乎在所有地方都添加了“wait”,它一直是我到目前为止遇到的所有问题的解决方案
谁能看出我做错了什么
谢谢 查看您的代码,我最初的想法是您的
SynchronizeClientandServer()
在您的将语言添加到Selection()之前完成但是,在synchronizeclientandserver()内部
您有xhr.onreadystatechange
,它是异步的,在程序的其余部分继续时等待服务器响应
本质上,顺序是这样运行的:#1、#2、#3、#2.1(xhr.onreadystatechange
)、#4
一个解决方案可能是从内部调用addLanguagesToSelection()
。xhr.onreadystatechange
wait
承诺,不要把关键词散布到任何地方。@Quentin说到“wait”,我通常都很吝啬s:-)这只是在每个可能的地方添加它们,直到解决问题,然后在发现问题后删除它们。我从来没有进入第二阶段。它们现在将被移除!是的,你完全正确!我确信问题出在dexie db的异步性上,所以我没有去看其他地方。。。非常感谢你!
async function initialiseDb ()
{
await database.version ( 1 ).stores ( { values_lookup: '++name, value' } );
await database.version ( 1 ).stores ( { languages: '++id, identifier, name' } );
languagesTable = await database.languages;
valuesLookupTable = await database.values_lookup;
// Set synchronisation millisecond = 0 if the database has just been created
if ( await valuesLookupTable.where ( 'name' ).equals ( "synchronisationMs" ).count () == 0 )
{
await valuesLookupTable.put ( { name : 'synchronisationMs', value : 0 } );
}
console.log ( "> 111 > valuesLookupTable initialised : " + await valuesLookupTable.where ( 'name' ).equals ( "synchronisationMs" ).count () );
console.log ( "> 112 > valuesLookupTable initialised : " + await languagesTable.count () );
}
async function synchroniseClientAndServer ()
{
var previousSyncTime = ( await valuesLookupTable.where ( 'name' ).equals ( "synchronisationMs" ).first () ).value;
var syncData = JSON.stringify ( { previousSynchronisationTime : previousSyncTime } );
var xhr = new XMLHttpRequest();
xhr.open ( "POST", service + " ClientServerSynchronisation", true );
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8" );
xhr.send ( syncData );
xhr.onreadystatechange = async function ()
{
// Redirect to home page if sessioin is not active
if ( this.status === 418 )
{
goHome ();
}
else if ( this.readyState === 4 && this.status === 200 )
{
var syncData = JSON.parse ( xhr.responseText );
// Update last synchronisation time for the next request
await database.values_lookup.put ( { name : 'synchronisationMs', value : syncData.synchronisationTime } );
// Update the list of languages with newly added and deleted
var updatedLanguages = syncData.updatedLanguages;
for ( var index = 0; index < updatedLanguages.length; index = index + 1 )
{
var language = updatedLanguages [ index ];
if ( language.addedOrDeleted === ADDED )
{
await languagesTable.put ( { id : language.id, identifier : language.identifier, name : language.name } );
console.log ( "> 200 > valuesLookupTable initialised : " + await languagesTable.count () );
}
else
{
await languagesTable.delete ( language.id );
console.log ( "> 200 > valuesLookupTable initialised : " + await languagesTable.count () );
}
}
}
};
}
async function addLanguagesToSelection ()
{
console.log ( "> 300 > " );
var selector = ge ( "languageId" );
var languages = await languagesTable.toArray ();
....
}
> 111 > valuesLookupTable initialised : 1 desktop_js.jsp:720:13
> 112 > valuesLookupTable initialised : 0 desktop_js.jsp:721:13
> 300 > desktop_js.jsp:501:10
> 200 > valuesLookupTable initialised : 1 desktop_js.jsp:409:17
> 200 > valuesLookupTable initialised : 2 desktop_js.jsp:409:17
> 200 > valuesLookupTable initialised : 3 desktop_js.jsp:409:17
> 200 > valuesLookupTable initialised : 4 desktop_js.jsp:409:17
> 200 > valuesLookupTable initialised : 5 desktop_js.jsp:409:17
> 200 > valuesLookupTable initialised : 6 desktop_js.jsp:409:17
> 200 > valuesLookupTable initialised : 7 desktop_js.jsp:409:17