Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs wait是typescript中的保留字吗?_Reactjs_Typescript - Fatal编程技术网

Reactjs wait是typescript中的保留字吗?

Reactjs wait是typescript中的保留字吗?,reactjs,typescript,Reactjs,Typescript,我想使用异步等待。但它给出的等待是一个保留字错误。我的代码是: public componentDidMount() { this.drags(); } private drags = async () => { const e = ReactDOM.findDOMNode(this.container); if (e) { e.addEventListener("mousedown", (event: any) => { a

我想使用异步等待。但它给出的等待是一个保留字错误。我的代码是:

public componentDidMount() {
    this.drags();
}
private drags = async () => {
       const e = ReactDOM.findDOMNode(this.container);
    if (e) {
      e.addEventListener("mousedown", (event: any) => {
        await this.dragElement.classList.add("draggable");
        await this.resizeElement.classList.add("resizable");
       }
    }

在开始一个问题时,最好用一个简单但完全有效的问题示例。这是起点,包括错误

声明var-ReactDOM:any

class Example {
    dragElement: any;
    resizeElement: any;
    container: any;

    public componentDidMount() {
        this.drags();
    }

    private drags = async () => {
        const e = ReactDOM.findDOMNode(this.container);
        if (e) {
            e.addEventListener("mousedown", (event: any) => {
                await this.dragElement.classList.add("draggable");
                await this.resizeElement.classList.add("resizable");
            });
        }
    }
}
TypeScript编译器实际上通过以下消息识别此错误:

“await”表达式仅允许在异步函数中使用

这直接指向了问题所在:函数不是异步的,因此不能使用
async
关键字

异步的 解决方案一——使其异步

declare var ReactDOM: any;

class Example {
    dragElement: any;
    resizeElement: any;
    container: any;

    public componentDidMount() {
        this.drags();
    }

    private drags = async () => {
        const e = ReactDOM.findDOMNode(this.container);
        if (e) {
            e.addEventListener("mousedown", async (event: any) => {
                await this.dragElement.classList.add("draggable");
                await this.resizeElement.classList.add("resizable");
            });
        }
    }
}
允诺 解决方案二-使用承诺。。。我之所以包含这一点,是因为您可以将异步语法与承诺语法进行比较

declare var ReactDOM: any;

class Example {
    dragElement: any;
    resizeElement: any;
    container: any;

    public componentDidMount() {
        this.drags();
    }

    private drags = async () => {
        const e = ReactDOM.findDOMNode(this.container);
        if (e) {
            e.addEventListener("mousedown", (event: any) => {
                Promise.all([
                    this.dragElement.classList.add("draggable"),
                    this.resizeElement.classList.add("resizable")
                ])
                    .then((result) => { /* Anything you need to do after */ })
                    .catch((reason) => { throw reason });
            });
        }
    }
}

. 不过,我对打字脚本不在行。解决方案是将此e.addEventListener(“mousedown”(事件:any)=>{替换为e.addEventListener(“mousedown”),async(事件:any)=>{
Wait
只能在
async
函数中使用。您的
mousedown
处理程序不是
async