1
function bsfir() {
    Parse.Promise.as().then(function() {
        return Parse.Cloud.run('gRFI', {});
    }).then(function(gRFIr) {
        return Parse.Cloud.run('gFI', { });
    }).then(function(gFIR) {
        return gFIR;
    }, function(error) {
        return error;
    });
}

Parse.Cloud.define("bSFI", function(request, response) {
    Parse.Promise.as().then(function() {
        return bsfir();
    }).then(function(bsfirr) {
        response.success(bsfirr);
    }, function(error) {
        response.error("219 error: " + JSON.stringify( error));
    });
});

The goal is to have bsfir() complete execution (i.e. resolve the promise) before handing execution back to the caller, in this case, bSFI().

Calling bSFI() results in executing only a few lines of code in bsfir(). The execution of bSFI() completes almost immediately/instantaneously probably because the promise in bsfir() isn't tied to a return value so when bSFI() calls bsfir(), execution immediately goes to response.success(bsfirr);

Is using Promise.all() in bsfir() a good solution?

domdomcodecode
  • 2,355
  • 4
  • 19
  • 27
Henry Situ
  • 153
  • 14
  • 3
    No, you cannot do that. All you can do is return a promise. There is no way to make an asynchronous action synchronous. – Bergi Oct 28 '15 at 23:48
  • Thanks Bergi, returning a promise will work also. Only a 1 or 2 lines of code in bsfir() are executed in the split second that it takes to call bsfir() then execution continues in the caller, & the rest of the code in bsfir() don't get a chance to be run. In many other scenarios, it works perfectly if I were to change bsfir() into cloud code. bSFI() will call bsfir() and after bsfir() completes 100%, the return value from bsfir() is returned to bSFI() then execution will continue in bSFI() as expected. But since bsfir() is a regular javascript function, it's not behaving as expected. – Henry Situ Oct 28 '15 at 23:56

1 Answers1

2

No, you cannot wait for the promise to resolve before you return. Your task is asynchronous, but the return must happen immediately.

But what you can do is to simply return the promise (from your) itself, and allow your caller to wait for it. In fact, your bSFI() already uses promise anyway, so it is trivial to integrate.

function bsfir() {
    return Parse.Promise.as().then(function() {
//  ^^^^^^
        return Parse.Cloud.run('gRFI', {});
    }).then(function(gRFIr) {
        return Parse.Cloud.run('gFI', {});
    });
}

Parse.Cloud.define("bSFI", function(request, response) {
    Parse.Promise.as().then(bsfir).then(function(bsfirr) {
        response.success(bsfirr);
    }, function(error) {
        response.error("219 error: " + JSON.stringify( error));
    });
});

Notice that you should be able to replace Parse.Promise.as().then(…) by just …() in both functions.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thanks Bergi, do you have time to work on a bigger project long term? – Henry Situ Oct 29 '15 at 00:54
  • 1
    @HenrySitu: Sorry, I'm not for hire if you mean that, and not in LA for sure. – Bergi Oct 29 '15 at 01:01
  • 1
    @Bergi - am I missing something? The first, empty promise in the chain seems to me superfluous. Why not `return Parse.Cloud.run('gRFI', {}).then() {...` ? Cloud.run() returns a promise. That should be the start of the chain, right? – danh Oct 29 '15 at 05:02
  • @Bergi thanks. If anything comes up, please let me know. We can always contribute to a project virtually together using something like github. – Henry Situ Oct 29 '15 at 06:28
  • 1
    @danh: Yes, it's superfluous, that's what I meant by my last paragraph. I just didn't want to change the OP's code too much, so that it is more obvious where the `return` was missing. – Bergi Oct 29 '15 at 09:36
  • Thanks @Bergi Appreciate it. – Henry Situ Oct 29 '15 at 15:57