简体   繁体   English

类中的异步/等待:意外令牌`this`

[英]Async/Await in a Class : unexpected token `this`

I am experimenting with async/await, I can't understand why this line : 我正在尝试异步/等待,我不明白为什么这行:

resolvedValue = await this.tryToSolve()

gives me this error : 给我这个错误:

Unexpected token this 意外的令牌

 class Test { constructor() { this.method = 0 this.checkLink() } async checkLink() { return new Promise((resolve, reject) => { let resolvedValue for (let i = 0; i < 3; i++) { this.method = i resolvedValue = await this.tryToSolve() if (resolvedValue) break } console.log(`Method ${this.method} did the trick.`); resolve(resolvedValue) }) } tryToSolve() { return new Promise((resolve, reject) => { // Resolves if this.method==1 console.log(`Trying to solve with method ${this.method}...`); setTimeout(() => { resolve(!!this.method ? `http://www${this.method}.someurl.com` : false) }, 1000) }) } } const test = new Test() 

Does anyone know the correct syntax to store the result of an async method in a variable? 有谁知道将异步方法的结果存储在变量中的正确语法?

Thanks in advance. 提前致谢。

To keep things simple, it happens because when you create a Promise, in its' constructor you pass an arrow function, which contains await call. 为简单起见,它的发生是因为创建Promise时,在其构造函数中传递了一个箭头函数,该函数包含await调用。 You must always put async keyword before the declaration of a function, that contains await . 您必须始终在包含await的函数声明之前放置async关键字。

So, instead of doing this 所以,不要这样做

async checkLink() {
    return new Promise((resolve, reject) => {

        let resolvedValue

        for (let i = 0; i < 3; i++) {
            this.method = i
            resolvedValue = await this.tryToSolve()
            if (resolvedValue) break
        }
        console.log(`Method ${this.method} did the trick.`);
        resolve(resolvedValue)
    })
}

Do it like this 像这样做

checkLink() {
    return new Promise(async (resolve, reject) => {

        let resolvedValue

        for (let i = 0; i < 3; i++) {
            this.method = i
            resolvedValue = await this.tryToSolve()
            if (resolvedValue) break
        }
        console.log(`Method ${this.method} did the trick.`);
        resolve(resolvedValue)
    })
}

More info: https://ponyfoo.com/articles/understanding-javascript-async-await#using-async-await 更多信息: https//ponyfoo.com/articles/understanding-javascript-async-await#using-async-await

Drop the new Promise around the await ! new Promise放在await You want only 你只想要

async checkLink() {
    let resolvedValue
    for (let i = 0; i < 3; i++) {
        this.method = i
        resolvedValue = await this.tryToSolve()
        if (resolvedValue) break
    }
    console.log(`Method ${this.method} did the trick.`);
    return resolvedValue;
}

or much simpler 或更简单

async checkLink() {
    for (let i = 0; i < 3; i++) {
        const value = await this.tryToSolve()
        if (value) {
            console.log(`Method ${i} did the trick.`);
            return value;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM