在react中使用async,我想先发送0,0,0,如果state中quantity发送变化,再发送quantity,报错

import React from 'react';
import './styles'

class Basic extends React.Component {

    constructor() {
        super();
        this.state = {
            quantity: 0 //基本信息
        }
    }

    render() {
        return (
            <div>
                <div className={'telecommunication_index'}>

                </div>

            </div>
        )
    }

    transmission(massage) {
        const _self = this;
        console.log(massage);
        setTimeout(()=> {
             _self.setState({
                 quantity: 100
             })
        },800);
    }

    async timer(){
        const _self = this;
        return new Promise((resolve,reject) => {
            setTimeout(()=> {
                _self.transmission('0,0,0')
                resolve('800之后出现了');
            },800);
        });
    }

    componentDidMount() {
        var key=false;
        new Promise(function(resolve, reject){
            if(key){
                resolve('成功了')
            }else{
                reject('被拒绝')
            }
        }).then(function(value) {
            console.log(value); // key=true 123
        }


        this.timer.then(result=> {
            if(this.state.quantity == 'change'){
                this.transmission(this.state.quantity)
                console.log(result);  //500
            }
        }).catch(err=> {
            console.log(err.message);
        });

    }


}

export default Basic;

传不了图片

阅读 1.3k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题