gpt4 book ai didi

使用 dexie db 时以意外顺序调用 Javascript 函数

转载 作者:行者123 更新时间:2023-12-01 00:28:00 24 4
gpt4 key购买 nike

我有一个在加载页面时执行的函数,该函数调用其他四个函数:

// 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 数据库初始化

2-从服务器同步数据,将数据保存到本地表

3 - 使用本地表中保存的数据。

4 - 以 5 秒的间隔执行 #2 的循环

由于某些原因,它们按照#1 #3 #2和#4的顺序执行

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

因此语言不会出现,因为列表是在加载数据之前填充的。我知道问题可能是由于 dexie db 是异步的,但我几乎在所有地方都添加了“await”,并且它一直是我迄今为止遇到的所有问题的解决方案。

有人能发现我做错了什么吗?

谢谢!

最佳答案

查看您的代码,我最初的想法是您的 synchroniseClientAndServer() addLanguagesToSelection() 之前完成。 但是,在 synchroniseClientAndServer() 内部,您有 xhr.onreadystatechange ,它是异步的,在程序的其余部分继续运行时等待服务器响应。

本质上,顺序是这样运行的:#1, #2, #3, #2.1 (xhr.onreadystatechange), #4。

一种解决方案可能是从 xhr.onreadystatechange 内部调用 addLanguagesToSelection()

关于使用 dexie db 时以意外顺序调用 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58825325/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com