return Parse.Promise.as().then(function() { // this just gets the ball rolling
var promise = Parse.Promise.as(); // define a promise
_.each(data, function(item) { // use underscore, its better :)
promise = promise.then(function() { // each time this loops the promise gets reassigned to the function below
var query = new Parse.Query("Panorama");
query.equalTo("buildingCode", item);
return query.find().then(function(results) { // the code will wait (run async) before looping again knowing that this query (all parse queries) returns a promise. If there wasn't something returning a promise, it wouldn't wait.
console.log("lresults[i].get(Name):"+results.lenght);
if(results.length == 0){//item does not exists
console.log("not exists");
}else{ // item exists
for (var i = 0; i < results.length; ++i) {
console.log("results[i].get(Name): "+results[i].get("Name"));
returndata.push({"name":results[i].get("name"),"objectId":results[i].id});
// console.log("after arr.push");
}
}
return Parse.Promise.as(); // the code will wait again for the above to complete because there is another promise returning here (this is just a default promise, but you could also run something like return object.save() which would also return a promise)
}, function (error) {
response.error("score lookup failed with error.code: " + error.code + " error.message: " + error.message);
});
}); // edit: missing these guys
});
return promise; // this will not be triggered until the whole loop above runs and all promises above are resolved
}).then(function() {
response.success(returndata); // edit: changed to a capital A
}, function (error) {
response.error("script failed with error.code: " + error.code + " error.message: " + error.message);
});
var promise = Parse.Promise.as(); // define a promise
_.each(data, function(item) { // use underscore, its better :)
promise = promise.then(function() { // each time this loops the promise gets reassigned to the function below
var query = new Parse.Query("Panorama");
query.equalTo("buildingCode", item);
return query.find().then(function(results) { // the code will wait (run async) before looping again knowing that this query (all parse queries) returns a promise. If there wasn't something returning a promise, it wouldn't wait.
console.log("lresults[i].get(Name):"+results.lenght);
if(results.length == 0){//item does not exists
console.log("not exists");
}else{ // item exists
for (var i = 0; i < results.length; ++i) {
console.log("results[i].get(Name): "+results[i].get("Name"));
returndata.push({"name":results[i].get("name"),"objectId":results[i].id});
// console.log("after arr.push");
}
}
return Parse.Promise.as(); // the code will wait again for the above to complete because there is another promise returning here (this is just a default promise, but you could also run something like return object.save() which would also return a promise)
}, function (error) {
response.error("score lookup failed with error.code: " + error.code + " error.message: " + error.message);
});
}); // edit: missing these guys
});
return promise; // this will not be triggered until the whole loop above runs and all promises above are resolved
}).then(function() {
response.success(returndata); // edit: changed to a capital A
}, function (error) {
response.error("script failed with error.code: " + error.code + " error.message: " + error.message);
});
沒有留言:
張貼留言