박형렬
박형렬

Reputation: 397

How to extract output from async function - react-native?

I have got a question. How do I get a result from function a async function?

This is my code:

function kakaoLogin() {
  return async dispatch => {
    RNKakaoLogins.login((err, result) => {
      console.log(result);
    });
    console.log(result);
  };
}

this is result. enter image description here

The first console.log(result) shows token!. but second console.log(result) does not show anything.

I want to get same result from second console.log(result) as first console.log(result)

Upvotes: 0

Views: 491

Answers (1)

Khemraj Sharma
Khemraj Sharma

Reputation: 59004

function kakaoLogin() {
  return async dispatch => {
    RNKakaoLogins.login((err, result) => {
      console.log(result);
    });
    console.log(result); // calling result outside its scope will not work
  };
}

What do you expect with a null result? You are using result outside of block. If you want to do something in response. then you can make method call like.

function kakaoLogin() {
  return async dispatch => {
    RNKakaoLogins.login((err, result) => {
      console.log(result);
      doSomeWork(result);
      // you can call another method here when you get response.
    });
  };
}

function doSomeWork(result){
.. somework
}

Upvotes: 2

Related Questions