console log of returned value shows a unusable response even after chaining a then to it

Kenny Q

Im trying to return a promise is a javascript file. However, there is a weird issue. So when I console log the returned value within the function, it shows the following:

const id = getAccounts()
  .then(res => res.find(acc => acc.type === ACCOUNT_TYPES.STARTER))
  .then((res) => { return res.id });

  console.log(id.then(res => res))

enter image description here

Is there anything I am missing? Have been dealing with this and research for the whole day. If anyone can help, I would highly appreciate it!

Updated section:

const initialState = {
  currentAccountId: id.then((res) => { return res; }) || ''
};
Ross Allen

The return value of calling a Promise's .then is always another Promise. By setting currentAccountId to id.then, it will always be a Promise.

You need to call this.setState from inside the Promise's resolve function:

componentDidMount() {
  getAccounts()
    .then(res => res.find(acc => acc.type === ACCOUNT_TYPES.STARTER))
    .then((res) => { this.setState({ currentAccountId: res }); });
}  

Use componentDidMount, like the React docs suggest, to initiate an async request. "If you need to load data from a remote endpoint, this is a good place to instantiate the network request."


Original answer

id.then will always return a new Promise, and that's what you are logging. To log the actual value you can move the console.log inside the resolve function:

id.then(res => console.log(res))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

console log shows old value even after updating it inside .then method in JS

Javascript: a function returned a value but when I console.log it, always shows undefined

Console log shows 'undefined' for a specific value of an array

React state is undefined , even though console.log shows it

JS says an object is undefined even though it shows with console.log

fetching from a URL shows an error in console even after catching it?

console.log shows original array after foreach

when i fill a form input it shows the previous value in console log

select showing undefined but console log shows value angularjs

React key not working even after giving key to the returned value

Can't access object property, even though it shows up in a console log

React Axios get method is not displaying any data even though console.log output shows data

React Axios call is not displaying any data even though console.log output shows data

if statement doen't run even though the console.log before that shows the statement should run

c++ method chaining on returned value not working

Trying to get addMethod in jquery validation to work. console.log shows the proper response, but it always fails validation

Files download in Chrome, but not in Safari, but console.log(response) and Network tab shows the file being sent from the server

Function finished with status: 'ok' but console logs shows Function returned undefined, expected Promise or value

Why Gitbash console shows my user Chopped as the default Windows Username even after I changed it?

console.log() shows the changed value of a variable before the value actually changes

Why ActiveMQ doesn't and console still shows messages after deleting db-*.log files from KahaDB

Backend response value is empty when endpoint is called from React component even though network call shows response body

Can't access object property even though console.log clearly shows an Object with the needed property (React Redux)

(JsFiddle) Knockout observable shows different value in binding on screen to console.log

Trouble changing object value in page, but shows up in console log as working (angularjs)

Props passed to child component is undefined but console.log shows props' value

Params is nil even though log shows passing

Why isnt the final console.log showing the whole data even after waiting for all promises to resolve?

Reactjs object data showing undefined even after showing data in console.log