0

I have, what I believe to be a pretty normal and sane javascript function, below:

$scope.pickFile = function() {
    filepicker.setKey("...");
    filepicker.pick(
        {},
        function(Blob) {  // OnSuccess
            var uuid = Blob.url.split('/').slice(-1)[0];
            window.location.replace("/url" + uuid);
            $.post(
                "/other_url" + uuid,
                {'input': $('#rawText')[0].checked? "text" : "features"}
            );
    });
}

Now, the bizarre thing is that the redirect doesn't happen until the post request finishes. My gut says this might be some magic happening behind the scenes with filepicker, but I have no idea what it might be, and there doesn't seem to be any documentation around it.

It doesn't seem to be simple synchronicity either since the redirect is actually first, but it is blocking on the post request completing.

Slater Victoroff
  • 21,376
  • 21
  • 85
  • 144
  • at a cursory glance the `post` might need occur prior to the redirect with the `window.location` perhaps? (Or the redirect in the callback?) – Mark Schultheiss Aug 26 '16 at 17:07
  • @MarkSchultheiss Apologies, I'm not quite following. It has to happen after the POST request is sent, but not after the response, which is what's happening. – Slater Victoroff Aug 26 '16 at 17:14
  • I guess another way to rephrase is are you looking for something like `window.onbeforeunload = function(e) { do post here; };` – Mark Schultheiss Aug 26 '16 at 17:24
  • This is the context of where my thought are: http://stackoverflow.com/questions/2536793/does-changing-window-location-stop-execution-of-javascript – Mark Schultheiss Aug 26 '16 at 17:28
  • @MarkSchultheiss I think that's unrelated to my problem. It's waiting for the post request to complete, which is what I want to stop. It does this even if I remove the redirect altogether. – Slater Victoroff Aug 26 '16 at 17:29
  • Ok so if you do `$.post( "/other_url" + uuid, {'input': $('#rawText')[0].checked? "text" : "features"} ); return false;` does that address your issue (the return false?) – Mark Schultheiss Aug 26 '16 at 17:33
  • @MarkSchultheiss Unfortunately not. No change. I thought it was fixed for a second, but it just erred early. – Slater Victoroff Aug 26 '16 at 17:49

1 Answers1

1

Try to change the post to explicitly not include the callback handler:

var mypost = $.ajax({
  type: "POST",
  url:  "/other_url" + uuid,
  data:  {'input': $('#rawText')[0].checked? "text" : "features"}
});
return false;
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100