Pretty new to this stuff, so probably doing this wrong, but it is my understanding to get a number (in this case startTypeValue) rather than the promise of a number, I need a function something like this:
render() {...
var startTypeValue: number =0;
const startType = async (accessToken: string): Promise<Number> => {
const value = await startTypeGetPromise(accessToken) // how to unwrap the value inside this promise
startTypeValue= value.valueOf();
console.log("inner"+JSON.stringify(startTypeValue, undefined, 2));
return startTypeGetPromise(accessToken)
}
const startTypePromise = startType(this.props.accessToken);
console.log("outer"+JSON.stringify(startTypeValue, undefined, 2));
inner gives 1 which is correct but outer gives 0. ( startTypeGetPromise(accessToken) does a DB call which returns 1). Is this because it is async and doesn't get the correct value in time? Or am I not setting the outer value correctly?
EDIT: following @ultrayam I added:
class myPage extends React.Component<MyPageProps> {
constructor(props: MyPageProps| any) {
super(props);
this.state = { startTypeValue: 0 };
}
but I try:
this.state['startTypeValue']
and I get:
Element implicitly has an 'any' type because expression of type '"startTypeValue"' can't be used to index type 'Readonly<{}>'.
.then()answers and just make auseEffect()with an async function, so you can useawaitinstead ofthenchaining (which is a little outdated). Your component should render aloading...screen until theuseEffect()has finished.