0

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)

1 Answer 1

2
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
}
Sign up to request clarification or add additional context in comments.

6 Comments

Also you shouldn't be using an async result immediately after issuing an async call. You should be using a callback like @Khemraj suggests or a then() method for better readability.
I want to get token value from second console.log(result) like first console.log(result)
The first console.log(result); is executed after the async method resolves with the result. The second console.log(result); is executed with the result variable being undefined.
How will you use response when response did not come?
Your code executes like this RNKakaoLogins.login -> second console.log(result); -> RNKakaoLogins.login returns result -> first console.log(result);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.