在云代码中,连续多次调用Parse.Cloud.run函数 [英] In cloud code, call a Parse.Cloud.run function more than once in series

查看:87
本文介绍了在云代码中,连续多次调用Parse.Cloud.run函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说您有一个定义" Parse.com云代码功能...

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

可以做到吗?

推荐答案

我认为这应该可以解决问题!

I think this should do the trick!

在这种情况下,我们要使用Parse.Promise.when()

In this scenario we want to use Parse.Promise.when()

返回所有输入的Promise都满足的新Promise 解决了.如果列表中的任何承诺失败,则返回 许诺将失败,并返回最后一个错误.如果他们都成功了,那么 返回的诺言将成功,其结果是 所有的输入承诺

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

这篇关于在云代码中,连续多次调用Parse.Cloud.run函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆