0

I am trying to return an object at the end of a chain of promises:

var signUp = function(fb_ID,fb_accessToken) {

    console.log("signup")

    var TokenStorage = Parse.Object.extend("tokenStorage");
    var restrictedAcl = new Parse.ACL();
    var output = {}
    var username;
    var password;

    return new Promise(function(resolve){ 
        ExtendFacebookAccessToken(fb_accessToken).then(function(new_fb_accessToken){

            restrictedAcl.setPublicReadAccess(false);
            restrictedAcl.setPublicWriteAccess(false);
            var user = new Parse.User();

            // Generate a random username and password.
            /*
            var username = new Buffer(24);
            var password = new Buffer(24);
            _.times(24, function(i) {
                username.set(i, _.random(0, 255));
                password.set(i, _.random(0, 255));
            });
            */

            username = (Date.now().toString(36) + Math.random().toString(36).substr(2, 10)).toUpperCase();
            password = (Date.now().toString(36) + Math.random().toString(36).substr(2, 10)).toUpperCase();

            user.set("username", username.toString('base64'));
            user.set("password", password.toString('base64'));

            // Sign up the new User
            return user.signUp().then(function(user) {
                console.log('user:'+JSON.stringify(user))
                // create a new TokenStorage object to store the user+GitHub association.
                var ts = new TokenStorage();
                ts.set('facebookID', fb_ID);
                ts.set('fb_accessToken', new_fb_accessToken);
                ts.set('username',username)
                ts.set('password',password)
                ts.set('user', user);

                ts.setACL(restrictedAcl);
                // Use the master key because TokenStorage objects should be protected.
                return ts.save(null, { useMasterKey: true } );
            }).then(function(tokenStorage) {
                console.log('ggg:')
                //var promise = new Parse.Promise();
                output.username = username;
                output.password = password;
                output.process = "signup";
                output.fb_accessToken = new_fb_accessToken;
                output.success = true;
                output.isnewuser = true;
                //promise.resolve(output);
                //return output
            });
        })
    })
    resolve(output)     
}

the 'output' object is not returned when I called the function using 'then': signUp(fb_ID,fb_accessToken).then(function(data){ console.log('data'+JSON.stringify(data)) }) the data object is not returned at the 'output' object inside the SignUp function. Any idea?

RPichioli
  • 3,245
  • 2
  • 25
  • 29
asasa178
  • 221
  • 1
  • 11
  • 1
    first of all..as the ExtendFacebookAccessToken is returning a promise..you dont need to create a "new Promise". Just return the ExtendFacebookAccessToken(fb_accessToken).then() object. – nikhil Sep 20 '16 at 16:33
  • and then un-comment the "return output" line. then try. Dont forget to remove the "resolve(output)" line too. – nikhil Sep 20 '16 at 16:34
  • something like [jsfiddle](https://jsfiddle.net/pcazuf01/) – nikhil Sep 20 '16 at 16:38
  • As nikhil said, avoid the [`Promise` constructor antipattern](http://stackoverflow.com/q/23803743/1048572)! – Bergi Sep 20 '16 at 16:59
  • It's `resolve(output)` at best, not `promise.resolve(output)`. And you are forgetting to reject the returned promise in case an error happens, that is why you didn't even notice your mistake. – Bergi Sep 20 '16 at 17:01

0 Answers0