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

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

// 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的顺序执行

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”,这一直是我迄今为止遇到的所有问题的解决方案。

有人可以发现我在做什么错吗?

谢谢!

约翰·R

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章