0

I have already read this question How to access JSON Object.$$state.value? and I applied the suggestion, but I still have a promise object as a return of the console.

This is the asynchronous function inside the service:

function getData(myUrl){
    var newref = new Firebase(myUrl);

    return $q(function(resolve, reject){
        newref.once("value", function(snapshot){
            var data = snapshot.val();
            var newUsername = data.username;
            resolve(newUsername);      
        }) 
    });
}

And this is the return of the service:

return{
    getUsername: function(){
        var url = userUrl();
        var promise = getData(url);
        return promise.then(function(whatIneed){
            return whatIneed ;
        })  
    }
};

In the controller I call "getUsername":

var user = CommonService.getUsername();
console.log(user);

I have this return in the console: Console log

I would like to ave the string"Damiano" (the value) insted of the promise object. Why I still have a promise object? Where is the code wrong?

Thanks in advance

Community
  • 1
  • 1
fabius
  • 55
  • 1
  • 8
  • "Why I still have a promise object?" — Because you **cannot return a value from an asynchronous function** (when that value isn't determined until the promise has resolved). You have to do the work in the `done`/`then` handler. That is the point of promises. – Quentin Feb 24 '16 at 15:47
  • @Quentin I want to access the value of the promise object... How can I change my code? – fabius Feb 24 '16 at 16:03
  • You pass a function to `then` that does whatever you want to do with the value. – Quentin Feb 24 '16 at 16:10
  • @Quentin I don't want to do anything with the value, I just want to return it. In the code there's "return whatIneed" but it returns a promise object. – fabius Feb 24 '16 at 16:14
  • See my first comment. Keep going around in circles until you give up and actually do the work in the function you pass to `then`. – Quentin Feb 24 '16 at 16:22
  • @Quentin Sorry for the ignorance but I really don't know what I have to write inside `then`. :( Can you answer to the question writing the code? – fabius Feb 24 '16 at 16:29
  • Based on what you wrote in the very last code block in the question: `promise.then(function(whatIneed){ console.log(whatIneed); });` – Quentin Feb 24 '16 at 16:30
  • Ok @Quentin, my problem is different: when I call `getUsername` in the controller I want that inside the variable `user` is stored the value of whatIneed. – fabius Feb 24 '16 at 16:37
  • And we're back to my first comment. You can't do that. You have to do the work in the function you pass to `then`. – Quentin Feb 24 '16 at 16:38
  • @Quentin, but here he does! http://andyshora.com/promises-angularjs-explained-as-cartoon.html – fabius Feb 24 '16 at 16:45
  • No, in that example the Controller receives a promise object, passes a function to `then` … and then does the work with the data inside that function. – Quentin Feb 24 '16 at 16:48
  • @Quentin so it's impossible to bring out the value of `whatIneed` from `then`. No workaround are possible? I really need to bring it out, I cannot do everything inside the `then`. – fabius Feb 24 '16 at 16:53
  • Correct. If you want to write asynchronous code then you have to refactor your program to work asynchronously. – Quentin Feb 24 '16 at 16:54
  • Thank you for the patience @Quentin ! <3 – fabius Feb 24 '16 at 17:12

0 Answers0