简体   繁体   中英

how do I properly fire this function only if the previous has successfully been completed?

I am attempting to do a series of actions that has a user logout then redirect them to a home page, using parse in Javascript. Here is the logic:

jQuery(document).ready(function() {
    $("#logout").on("click", function (e) {
      //logout current user
      if ( Parse.User.current() ) {
        Parse.User.logOut();

        // check if really logged out
        if (Parse.User.current()) {
          console.log("Failed to log out!");
        } else {
        // redirect page

          window.location.replace("home-screen.html");
        }
      }
    });
  });

However most times when I logout, back in my parse server, the user session hasn't been destroyed. But I noticed when I remove the window.location action, the user session is destroyed every single time successfully.

So how do I edit my function to say "on click, log user out and ONLY if done successfully THEN redirect page?

Parse.User.logout() returns a promise. Redirect the user after the promise is resolved successfully.

 jQuery(document).ready(function() { $("#logout").on("click", function (e) { //logout current user if ( Parse.User.current() ) { Parse.User.logOut() .then(function () { window.location.replace("home-screen.html"); }); } }); });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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