Javascript 等待函数响应,然后更改componentDidUpdate中的状态

Javascript 等待函数响应,然后更改componentDidUpdate中的状态,javascript,image,reactjs,Javascript,Image,Reactjs,我想检查componentDidUpdate函数中是否存在图像,并返回true或false componentDidUpdate(prevProps){ if(prevProps.loadedGroup !== this.props.loadedGroup || prevProps.loadedGroupErrors !== this.props.loadedGroupErrors){ let image = false;

我想检查componentDidUpdate函数中是否存在图像,并返回true或false

    componentDidUpdate(prevProps){
    if(prevProps.loadedGroup !== this.props.loadedGroup 
        || prevProps.loadedGroupErrors !== this.props.loadedGroupErrors){
            let image = false;
            if(this.props.loadedGroup.picture !== null){
                if(this.props.loadedGroup.picture.fullResolutionPicName !== undefined){
                        //image = true; //have to be true to print image - false showing placeholder
                        image = checkImage(apiPicturesUrl + this.props.loadedGroup.picture.fullResolutionPicName)
                 }
            }

        this.setState({loadedData: this.props.loadedGroup, 
            loadingGroupDataSpinner: false, loadedPosts: this.props.loadedGroup.posts ,
            showBackdrop: false, openEditPlace: false, openEditPlaceDesc: false, 
            bgImageLoadedSucces: image});        
    }
}
还有我的checImage函数

function checkImage(src){
var image = new Image(); 
image.src = src;
    if (image.width === 0) {
        alert("no image");
        return false;
    }else{
        return true;
    }
}
这是一个问题,因为该函数是异步执行的,当我检查img width时,即使有图像,它也没有更新


如果没有
setTimeout
,如何正确地编写它?

很快-使用
image.onload
事件

//替换此代码
image=checkImage(apicturesurl+this.props.loadedGroup.picture.fullResolutionPicName)
//与
图像=新图像();
image.onload=()=>this.setState({
//在这里,您可以确保已加载该图像,并使用所需的数据设置组件状态
});
image.src=apicturesurl+this.props.loadedGroup.picture.fullResolutionPicName;
//请注意,“onload”和“src”行必须按此顺序正确写入。

//首先注册事件处理程序,然后设置“src”
是否尝试使用
async/await
?声明您的生命周期方法,如:
async componentdiddupdate(prevPorps)
,然后等待您的函数