Wait until a condition is true?

前端 未结 7 1918
日久生厌
日久生厌 2020-12-09 08:32

I\'m using navigator.geolocation.watchPosition in JavaScript, and I want a way to deal with the possibility that the user might submit a form relying on locatio

相关标签:
7条回答
  • 2020-12-09 09:28
    class App extends React.Component {
    
    componentDidMount() {
       this.processToken();
    
    }
     processToken = () => {
        try {
            const params = querySearch(this.props.location.search);
            if('accessToken' in params){
                this.setOrderContext(params);
                this.props.history.push(`/myinfo`);
            }
        } catch(ex) {
          console.log(ex);
        }
        }
    
       setOrderContext (params){
         //this action calls a reducer and put the token in session storage
         this.props.userActions.processUserToken({data: {accessToken:params.accessToken}});
    }
    
    render() {
        return (
    
            <Switch>
                //myinfo component needs accessToken to retrieve my info
                <Route path="/myInfo" component={InofUI.App} />
            </Switch>
    
        );
    }
    

    And then inside InofUI.App

    componentDidMount() {
            this.retrieveMyInfo();
        }
    
        retrieveMyInfo = async () => {
            await this.untilTokenIsSet();
            const { location, history } = this.props;
            this.props.processUser(location, history);
        }
    
        untilTokenIsSet= () => {
            const poll = (resolve) => {
                const { user } = this.props;
                const { accessToken } = user;
                console.log('getting accessToken', accessToken);
                if (accessToken) {
                    resolve();
                } else {
                    console.log('wating for token .. ');
                    setTimeout(() => poll(resolve), 100);
                }
            };
            return new Promise(poll);
        } 
    
    0 讨论(0)
提交回复
热议问题