gpt4 book ai didi

Javascript - 使用 Promise 运行 for 循环

转载 作者:行者123 更新时间:2023-11-28 07:57:04 24 4
gpt4 key购买 nike

我有一个在 parse.com 上运行的 JavaScript 脚本,该脚本向用户发送推送通知。我的剧本快完成了。现在它会发送一个带有查询集的推送到我拥有它的位置。所以这是我现在对此的限制。

testItemsQuery.equalTo('school', 'Union College (NY)'); 
testItemsQuery.equalTo('meal','LUNCH');

但现在我有两个变量

var schoolArray = ['Union College (NY)', 'University of Albany'];
var mealArray = ['LUNCH','DINNER'];

我也希望它总共循环该脚本 4 次。我首先希望它循环遍历两所学校,然后循环遍历每顿饭。所以最基本的代码版本应该是这样的。

var schoolArray = ['Union College (NY)', 'University of Albany'];
var mealArray = ['LUNCH','DINNER'];

for (var i = 0; i < schoolArray.length; i++) {
for (var j = 0; j < mealArray.length; j++) {
//insert all my other code here
}
}

这是我的脚本,它向 parse.com 发出异步请求,这就是 for 循环不起作用的原因,因为我认为我需要使用一些 promise 。这是我想要在 for 循环中的代码:

    function SendPush(installationId, msg) {

var query = new Parse.Query(Parse.Installation);
query.equalTo('objectId', installationId);
Parse.Push.send({
where: query,
data: {alert: msg}
});
}
var schoolArray = ['Union College (NY)', 'University of Albany'];
var mealArray = ['LUNCH','DINNER'];

var TestItem = Parse.Object.extend("TestItem");
var UserFavorites = Parse.Object.extend("UserFavourite");
var testItemsQuery = new Parse.Query(TestItem);


testItemsQuery.equalTo('school', 'Union College (NY)'); //I want to loop through both schools for both meals
testItemsQuery.equalTo('meal','LUNCH');


var groupedAlerts = {};
testItemsQuery.each(function(testItem) {
var item = testItem.get('item');
var school = testItem.get('school');
var userFavoritesQuery = new Parse.Query(UserFavorites);
userFavoritesQuery.include('user');
userFavoritesQuery.equalTo('item', item);
userFavoritesQuery.equalTo('school', school);

return userFavoritesQuery.each(function(userFavorite) {

//console.log(userFavorite);
var user = userFavorite.get('user');
if(user){
var instId = user.get('installation').id;
//console.log(instId);
if(!groupedAlerts[instId]) {
groupedAlerts[instId] = [];
}
var meal = testItem.get('meal').toLowerCase();
var m = userFavorite.get('item') + " is being served for " + meal + " at " + testItem.get('schoolMenu');
groupedAlerts[instId].push(m);
}
});
}).then(function () {
var alerts = [];
for(var key in groupedAlerts) {
alerts.push({
installationId: key,
alerts: groupedAlerts[key],
});
// Send push notifications
SendPush(key, groupedAlerts[key].join());
}
});

提前感谢您的帮助!!!!

最佳答案

您只需要正确地确定您在这里标记的//所有其​​他代码的范围即可。如果将其包装在函数中:

function schoolMealPush(school, meal) {
// Really all your other code here
}

然后在你的内部 for 循环中调用该函数

for (var i = 0; i < schoolArray.length; i++) {
for (var j = 0; j < mealArray.length; j++) {
schoolMealPush(schoolArray[i], mealArray[j]);
}
}

你应该做生意。不需要 promise ……尽管可以使用 promise 来知道所有推送何时完成。只需返回 testItemsQuery.each 创建的 Promise 并将每个返回的 Promise 存储在数组中。然后使用任何 promise 库你可以做一个 .all

var promiseArray = [];
for (var i = 0; i < schoolArray.length; i++) {
for (var j = 0; j < mealArray.length; j++) {
promiseArray.push(schoolMealPush(schoolArray[i], mealArray[j]));
}
}
bluebird.all(promiseArray)
.spread( function(){
console.log('SendPush called for all values');
});

关于Javascript - 使用 Promise 运行 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25964180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com