0

I have a problem with the following chain of promises:

Parse.Cloud.run('cloudlogin', {
        fb_accessToken: $localStorage.accessTokenFacebook
        , facebookID: FACEBOOKID
    }, {
        success: function (userdata) {
            alert(JSON.stringify(userdata))
            $localStorage.username = userdata.username;
            $localStorage.password = userdata.password;
            $localStorage.fb_access_token = userdata.fb_accessToken;
            var bool = userdata.isnewuser
            alert('bool' + bool)
            return bool
        }
        , error: function (error) {
            alert(error)
            $state.go("login")
                .then(function () {
                    $ionicLoading.hide()
                })
        }
    })
    .then(function (isnewuser) {
        $localStorage.organizerAccess = true;
        alert('fbdata' + JSON.stringify(isnewuser))
    })

I would like to make the first promise to return the boolean 'isnewuser' to the second promise but instead the whole 'userdata' object is returned. Any idea?

Fadhly Permata
  • 1,686
  • 13
  • 26
asasa178
  • 221
  • 1
  • 11
  • can't you just use `userdata.isnewuser`? It seems you already have the property you're looking for in the next promise. – Aᴄʜᴇʀᴏɴғᴀɪʟ Sep 21 '16 at 10:47
  • yes I can obviously, but it's not the right solution.... the problem is why the 'return bool' does not return what is supposed to do – asasa178 Sep 21 '16 at 10:50
  • I believe this has something to do with `Parse.Cloud.run`'s implementation of Promises. A standard HTML Promise would work as you expect, but you're just inputting functions into `Parse.Cloud.run` which probably has its own implementation of a Promise, not a vanilla HTML Promise. – Aᴄʜᴇʀᴏɴғᴀɪʟ Sep 21 '16 at 11:01

3 Answers3

0

as the documentation says - then(successCallback, [errorCallback], [notifyCallback]) – regardless of when the promise was or will be resolved or rejected, then calls one of the success or error callbacks asynchronously as soon as the result is available. The callbacks are called with a single argument: the result or rejection reason. Additionally, the notify callback may be called zero or more times to provide a progress indication, before the promise is resolved or rejected.

so, instead of taking the returned value then takes the object returned which caused the success: function (userdata) which is userdata

Abbas Kararawala
  • 1,254
  • 12
  • 19
0

success is a callback function its return value won't pass to the next then. if you want to pass the bool value to next one need to rewrite code like below.

Parse.Cloud.run('cloudlogin', {
        fb_accessToken: $localStorage.accessTokenFacebook
        , facebookID: FACEBOOKID
     }, {
         error: function (error) {
            alert(error)
            $state.go("login")
                .then(function () {
                    $ionicLoading.hide()
                })
        }
    }).then(function (userdata) {
            alert(JSON.stringify(userdata))
            $localStorage.username = userdata.username;
            $localStorage.password = userdata.password;
            $localStorage.fb_access_token = userdata.fb_accessToken;
            var bool = userdata.isnewuser
            alert('bool' + bool)
            return bool
        })
    .then(function (isnewuser) {
        $localStorage.organizerAccess = true;
        alert('fbdata' + JSON.stringify(isnewuser))
    })
  • You probably should also avoid the `error` callback and instead pass it as the `onrejected` parameter to `then` – Bergi Sep 22 '16 at 02:20
0
login: function () {

    var deferred = $q.defer();

    Parse.Cloud.run('cloudlogin', {
        fb_accessToken: $localStorage.accessTokenFacebook,
        facebookID: FACEBOOKID
    }).error(function (error) {
        alert(error)
        $state.go("login")
        .then(function () {
            $ionicLoading.hide()
        })
    })
    .then(function (userdata) {
        alert(JSON.stringify(userdata))
        $localStorage.username = userdata.username;
        $localStorage.password = userdata.password;
        $localStorage.fb_access_token = userdata.fb_accessToken;
        deferred.resolve(userdata.isnewuser);
    });

    return deferred.promise;
}

write this function in service class and call it from controller

codephobia
  • 1,580
  • 2
  • 17
  • 42
Ram kumar D M
  • 11
  • 1
  • 4
  • This might become a good answer if you explain what you did change in the code and why. – Bergi Sep 22 '16 at 02:21
  • So,If we can create a service class we can call when ever we want basically this is angular js workflow.service class is interact with server code – Ram kumar D M Sep 22 '16 at 05:27