简体   繁体   English

在Parse云代码中使用自己的承诺

[英]Use own promises in Parse cloud code

I would like to save a registrationId which is generated randomly in Parse cloud code, so I need to check if that value is already in the DB, I have to do this in a recursive way until I get the right string. 我想保存一个在Parse云代码中随机生成的registrationId,所以我需要检查该值是否已经在DB中,我必须以递归的方式执行此操作,直到我得到正确的字符串。 Here is what I tried so far, the problem is that findRegistrationId() is not a promise so I cannot use then() is there any way to make it a promise or any other workaround? 这是我到目前为止尝试的,问题是findRegistrationId()不是一个承诺所以我不能使用then()有没有办法让它成为一个承诺或任何其他解决方法? for cloudcode 对于云代码

function getRandomString()

{
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
    var string_length = 4;
    var randomstring = '';

    for (var i=0; i<string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum,rnum + 1);
    }
    return randomstring;
}
function findRegistrationId()
{
    console.log("Enter findRegistrationId")
    var randomString = getRandomString();
    var query = new Parse.Query("Book");

    query.equalTo("registrationId", randomString);

    query.find.then(function(results){
        if(results.length === 0)
        {
            console.log("no registrationId duplicated")
            return randomString;
        }
        //if there is other registrationId we concatenate
        else
        {
            console.log("registrationId duplicated let's recursive it")
            return findRegistrationId();
        }
    },function(error){
        return error;
    })

}

// Use Parse.Cloud.define to define as many cloud functions as you want.
// Gets the unique cool BC identificator. The real glue of BC!
Parse.Cloud.define("GetBookId", function(request, response) {

    var promise = findRegistrationId();

    promise.then(function(result){

        console.log("success promise!!")
        response.success(result);

    }, function(error){
        console.error("Promise Error: " + error.message);

        response.error(error);

    });


});

You can write your function like this: 你可以写这样的函数:

function findRegistrationId()
{
    console.log("Enter findRegistrationId")
    var randomString = getRandomString();
    var query = new Parse.Query("Book").equalTo("registrationId", randomString);

    var promise = new Parse.Promise();

    query.find().then(function(results) {
        if (results.length == 0)
        {
            promise.resolve(randomString);
        }
        else
        {
            findRegistrationId().then(function(result) {
                promise.resolve(result);
            }, function(error) {
                promise.reject(error);
            });
        }
    }, function(error) {
        promise.reject(error);
    });

    return promise;
}

@arghbleargh posted a great solution, and I'm just posting a version that uses direct returning of promises rather than resolving/rejecting, which is an anti-pattern (see https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns eg): @arghbleargh发布了一个很好的解决方案,我只是发布一个使用直接返回promises而不是解析/拒绝的版本,这是一种反模式(参见https://github.com/petkaantonov/bluebird/wiki/Promise - 抗模式例如:)

function findRegistrationId()
{
    console.log("Enter findRegistrationId")
    var randomString = getRandomString();
    var query = new Parse.Query("Book").equalTo("registrationId", randomString);    

    return query.find().then(function(results) {
        if (results.length == 0)
        {
            return randomString;
            // or, you could return Parse.Promise.as(randomString);
            // but the explicit promise isn't required
        }
        else
        {
            findRegistrationId().then(function(result) {
                return result;
                // or, you could return Parse.Promise.as(result);
                // but the explicit promise isn't required
            }, function(error) {
                return Parse.Promise.error(error);
            });
        }
    }, function(error) {
        return Parse.Promise.error(error);
    });    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM