.then承诺sendMessage无效

时间:2016-11-16 07:08:56

标签: javascript jquery promise web-worker

我在webworker中有以下代码,

onmessage = function(e)
{
    var data;
    var message = e.data;
    sendTimeSeries = false;
    var semicolon = message.split(";");
    for (var i=0; i<semicolon.length;i++)
    {
        var colon= semicolon[i].split(":");
        for(var j=0; j<colon.length; j++)
        {
            if(colon[j] == "Insert TimeSeries")
            {
                stringTimeSeriesTableData = ""
                data = colon[j+1].split(" ");
                prevLetter = data[0];
                getAllRecords(prevLetter).then(addTimeSeries).then(getAllTimeSeries).then(sendMessage); //use of promises
            }
        }
    }
}

正在以正确的顺序调用方法getAllRecordsaddTimeSeriesgetAllTimeSeries(在前一个方法完成执行之前不会进入下一个方法。但是,{ {1}}在完成sendMessage方法之前调用了webworker方法。

getAllRecords():

getAllTimeSeries

addTimeSeries

function getAllRecords(letter)
{
    var deferred = $.Deferred();

    var trans = db.transaction(["ObservableStates"],"readonly").objectStore("ObservableStates").index('letterIndex');

    var request = trans.openCursor(IDBKeyRange.only(letter));

    request.onsuccess = function(event)
    {
        cursor = event.target.result;
        if(cursor)
        {
            count = count + 1;
            console.log(cursor.value);
            addRecordToHistory(cursor.value);
            deleteObservableStates(cursor.primaryKey);
            while(cursor.continue())
            {
            }
        }
        if(cursor==null)
        {
            console.log('ready from count');
            deferred.resolve();
        }
    }

    request.onerror = function(event)
    {
        console.log('ERROR LOADING DATA FROM TABLE');
    }
    return deferred.promise();

}

getAllTimeSeries()

function addTimeSeries()
{
    var deferred = $.Deferred();

    timeSeries = {
        l:prevLetter, t:count
    }
    request = db.transaction(['TimeSeries'], "readwrite").objectStore('TimeSeries').add(timeSeries);

    request.onerror = function(event)
    {
        console.log("Unable to add timeSeries to table TimeSeries");
    }
    request.onsuccess = function(event)
    {
        console.log("TimeSeries State Added");
        deferred.resolve();
    }

    return deferred.promise();
}

这里有什么我想念的吗?感谢

0 个答案:

没有答案