Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
azure异步javascript后端-等待函数_Javascript_Azure_Asynchronous_Backend_Azure Mobile Services - Fatal编程技术网

azure异步javascript后端-等待函数

azure异步javascript后端-等待函数,javascript,azure,asynchronous,backend,azure-mobile-services,Javascript,Azure,Asynchronous,Backend,Azure Mobile Services,我正在使用Azure移动服务和javascript后端。我的问题是该函数不等待其他函数的结束 我试着选择一个有分词规则的词。我想选择item.wordnumber最高的项目。如果没有几个项目具有相同的item.wordnumber,我想在其他表中选择与该项目关联的投票数最高的人 此脚本不等待函数CalcolateMaxAvg的返回。 我会像在c中那样使用Wait var tableWords = tables.getTable('Word'); var tableVotes = tables.g

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

我试着选择一个有分词规则的词。我想选择item.wordnumber最高的项目。如果没有几个项目具有相同的item.wordnumber,我想在其他表中选择与该项目关联的投票数最高的人

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

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;
}

问题是对表的调用。其中…读取。。。是异步的-它不会返回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;
}