Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

promise - In cloud code, call a Parse.Cloud.run function more than once in series

Say you have a "define" Parse.com cloud code function...

Parse.Cloud.define("exampleDefineFunction", function(request, response)
    {
    ...
    response.success("ok")
    ...
    response.error("doh")
    });

I want to make another cloud code function,

which calls that define function a number of times in series,

with one waiting for the next, much as in a serial promise chain

Can this be done?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...