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

查看:31
本文介绍了在云代码中,多次连续调用 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,

调用定义函数多次连续

一个等待下一个,就像在串行承诺链中一样

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 将因最后一个错误而失败.如果他们都成功了,那么返回的承诺会成功,结果是所有输入承诺

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天全站免登陆