0

I want to pass a value from the function to outside in Typescript.

I have tried many method, e.g. thing to declare global variables, return function value, etc, but it doesn’t work.

I think maybe my syntax is wrong. Could you please have a help? Code as below for reference. Many thanks.

    this.platform.ready().then(() => {

        FCMPlugin.getToken(
                    function (token) {
                        console.log(token);
                    },
                    function (err) {
                        console.log('error retrieving token: ' + err);
                    }
        );

        console.log(token); //I would to pass the token value to here
    }
Galaxy Lam
  • 53
  • 7
  • 1
    There is no way to move the code inside the success callback? It seems to me it will run your code before even getting the token possibly. – Sami Kuhmonen Sep 24 '16 at 17:27
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Tamas Hegedus Sep 24 '16 at 20:07

2 Answers2

0

What about define an external var?

Like this:

    this.platform.ready().then(() => { 

var myToken;

    FCMPlugin.getToken(
                function (token) {
                    console.log(token);

myToken= token;

                },
                function (err) {
                    console.log('error retrieving token: ' + err);
                }
    );

    console.log(myToken); //I would to pass the token value to here
}

If getToken is async, u could trigger an event or do a callback

    this.platform.ready().then(() => { 


    FCMPlugin.getToken(
                function (token) {
                    console.log(token);
                    myCallback(token);
                    $('html').trigger('tokenReady',token);
                },
                function (err) {
                    console.log('error retrieving token: ' + err);
                }
    );

  function myCallback(myToken){

    console.log(myToken);

  }

}

$('html').on('tokenReady',function(e,token){

myCallback(token);

});

third version (callback only):

    this.platform.ready().then(() => { 


    FCMPlugin.getToken(
                function (token) {
                    console.log(token);
                    myCallback(token);
                },
                function (err) {
                    console.log('error retrieving token: ' + err);
                }
    );

  function myCallback(myToken){

    console.log(myToken);

  }

}
Francesco
  • 555
  • 4
  • 23
0
FCMPlugin.getToken

Executes asynchronously so you will not have a value for token until you hit the get token callback.

Additionally as it executes asynchronously, your second console.log statement will fire before your callback so you will not have any way of reaching the token value as it has not been returned at that point.

Simple answer is to move your code to handle the returned token inside your success callback where you have your first console.log statement.

Banners
  • 247
  • 1
  • 7