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