azure异步javascript后端等待函数
azure asynchronous javascript backend - wait function
我使用的是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;
}
相关文章:
- JavaScript循环并等待函数
- 等待函数执行后再继续
- 嵌套的异步/等待函数
- 等待函数完成,然后将返回值分配给变量JS
- 如何在Node.js中等待函数完成后再继续
- 如何在JavaScript中创建一个等待函数,允许您在后台运行音频等内容
- 等待函数结束(在 async.series 中)无法按预期工作
- 等待函数 $.get 中的代码完成以继续
- Javascript等待函数结束,包括来自node.js的异步MYSQL查询
- 并行调用异步/等待函数
- Javascript 睡眠/延迟/等待函数
- 如何在 Node.js 中等待函数完成
- Javascript - 等待函数完成
- Javascript for loop不等待函数响应
- 等待函数完成对其他函数的调用
- 循环的Javascript,等待函数回调
- 如何使angular在继续之前等待函数返回值
- 如何让html在AngularJS控制器中等待函数
- Node.js强制等待函数完成
- azure异步javascript后端等待函数