azure异步javascript后端等待函数

azure asynchronous javascript backend - wait function

本文关键字:等待 函数 后端 javascript 异步 azure      更新时间:2023-09-26

我使用的是Azure移动服务和javascript后端。我的问题是这个函数不等待其他函数的结束。

我正在试着选择一个有分词规则的项目(单词)。我想选择项目数最高的项目。如果有几个项目有相同的项目。我想选择在该项目上拥有最高可用票数的项目(在另一个表"票数"中)。

此脚本不等待函数CalcolatMaxAvg的返回。我会像在c中那样等待。

var tableWords = tables.getTable('Word');
var tableVotes = tables.getTable('Votes');
var avgVotesActualWord = 0;
var maxItem = null;
var maxItemVote = 0;

function WordChoice() {
var select = tableWords.orderByDescending('wordnumber').read({success:
        function (results) 
        {
            results.forEach(function(item)
            {
                if(maxItem == null)
                {
                    maxItem = item;
                    maxItemVote = tableVotes.where({idword: item.id}).read({success: CalcolateMaxAvg});
                }
                else if(item.wordnumber > maxItem.wordnumber)
                {
                    maxItem = item;
                    maxItemVote = tableVotes.where({idword: item.id}).read({success: CalcolateMaxAvg});
                }
                else if(item.wordnumber == maxItem.wordnumber)
                {
                    //chack who have more votes
                    avgVotesActualWord = 0;
                    avgVotesActualWord = tableVotes.where({idword: item.id}).read({success: CalcolateMaxAvg});
                    //the problem is avgVoteActualWord that is always NaN
                    console.log('Word: %s with avg: %d', item.word, avgVotesActualWord);
                    if(avgVotesActualWord > maxItemVote)
                    {
                        //take the actualword because have more votes
                        maxItem = item;
                        maxItemVote = avgVotesActualWord;
                    }
                }
            })
            if(maxItem != null)
            {
                console.log('parola: %s', maxItem.word);
                maxItem.selected = true;
                tableWords.update(maxItem);  
            }
            else 
            {
                console.log('null');
            }
        }
    });
}

function CalcolateMaxAvg(resultsVote) 
{
    var sum = 0;
    var count = 0;
    var avg = 0;
    resultsVote.forEach(function(itemVote)
    {
        sum = sum + itemVote.vote;
        count = count + 1;
    })
    if(count > 0)
    {
        avg = sum / count;
    }
    //this is a correct value of avgVoteActualWord, but he don't wait the return of this value
    console.log('avg: %d', avg); 
    return avg;
}

问题是对table.where(...).read(...)的调用是异步的——它不会返回CalcolateMaxAvg函数返回的数字(它不会返回任何内容)。您需要重写代码以接受JavaScript的异步性,这与下面的代码大致相同。

var tableWords = tables.getTable('Word');
var tableVotes = tables.getTable('Votes');
var avgVotesActualWord = 0;
var maxItem = null;
var maxItemVote = 0;
function WordChoice() {
    var select = tableWords.orderByDescending('wordnumber').read({
        success: function (results)
        {
            function processNextResult(index) {
                if (index >= results.length) {
                    // All done
                    if(maxItem != null)
                    {
                        console.log('parola: %s', maxItem.word);
                        maxItem.selected = true;
                        tableWords.update(maxItem);  
                    }
                    else 
                    {
                        console.log('null');
                    }
                    return;
                }
                var item = results[index];
                if (maxItem == null) {
                    maxItem = item;
                    tableVotes.where({ idword: item.id }).read({ success: simpleProcessVotesResult });
                } else if (item.wordnumber > maxItem.wordnumber) {
                    maxItem = item;
                    tableVotes.where({ idword: item.id }).read({ success: simpleProcessVotesResult });
                } else if (item.wordnumber == maxItem.wordnumber) {
                    //check who have more votes
                    avgVotesActualWord = 0;
                    tableVotes.where({idword: item.id}).read({
                        success: function(resultsVote) {
                            avgVotesActualWord = CalcolateMaxAvg(resultsVote);
                            //the problem is avgVoteActualWord that is always NaN
                            console.log('Word: %s with avg: %d', item.word, avgVotesActualWord);
                            if(avgVotesActualWord > maxItemVote)
                            {
                                //take the actualword because have more votes
                                maxItem = item;
                                maxItemVote = avgVotesActualWord;
                            }
                            processNextResult(index + 1);
                        }
                    });
                } else {
                    processNextResult(intex + 1);
                }
            }
            function simpleProcessVotesResult(resultsVote) {
                maxItemsVote = CalcolateMaxAvg(resultsVote);
                processNextResult(intex + 1);
            }
            processNextResult(0);
        }
    });
}
function CalcolateMaxAvg(resultsVote) 
{
    var sum = 0;
    var count = 0;
    var avg = 0;
    resultsVote.forEach(function(itemVote)
    {
        sum = sum + itemVote.vote;
        count = count + 1;
    })
    if(count > 0)
    {
        avg = sum / count;
    }
    //this is a correct value of avgVoteActualWord, but he don't wait the return of this value
    console.log('avg: %d', avg); 
    return avg;
}