azure 异步 javascript 后端 - 等待函数

azure asynchronous javascript backend - wait function

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

我正在尝试选择具有特定规则的项目(单词)。我想选择 item.wordnumber 最高的项目。如果很少有相同 item.wordnumber 的项目,我想选择谁在该项目上的相关票数最高(在另一个 table "votes" 中)。

此脚本不等待函数 CalcolateMaxAvg 的 return。 我会像在 c# 中那样使用 await.

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(...) 的调用是异步的 - 它不会 return 一个数字 return 由 CalcolateMaxAvg 函数编辑(它不会return 任何东西)。您需要重写代码以接受 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;
}