I think this should do the trick!
In this scenario we want to use Parse.Promise.when()
Returns a new promise that is fulfilled when all of the input promises
are resolved. If any promise in the list fails, then the returned
promise will fail with the last error. If they all succeed, then the
returned promise will succeed, with the results being the results of
all the input promises
I've tried to make it fairly self-documenting but let me know if you have any questions. Hope this helps!
// The promise chain for bulk image collection
Parse.Cloud.define("fetchBulkImages", function(request, response) {
// Array of URLs
var urls = request.object.get("urls");
// Array of promises for each call to fetchImage
var promises = [];
// Populate the promises array for each of the URLs
_.each(urls, function(url) {
promises.push(Parse.Cloud.run("fetchImage", {"url":url}));
})
// Fulfilled when all of the fetchImage promises are resolved
Parse.Promise.when(promises).then(function() {
// arguments is a built-in javascript variable
// will be an array of fulfilled promises
response.success(arguments);
},
function (error) {
response.error("Error: " + error.code + " " + error.message);
});
});
// Your scraping function to load each individual image
Parse.Cloud.define("fetchImage", function(request, response) {
...
response.success("image successfully loaded")
...
response.error("Error: " + error.code + " " + error.message);
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…