简体   繁体   English

ESLint 发布 no-misused-promises,其中匿名函数用于等待多个异步调用

[英]ESLint issues no-misused-promises where anonymous function used to await multiple async calls

So I've been happily using these intermediary functions to wrap async operations that I need to operate sequentially.所以我一直很高兴地使用这些中间函数来包装我需要按顺序操作的异步操作。 Such as "ensure sqlite db exists, then create opening schema" etc. I Just switched over to ESLint and I can't figure out what syntax it wants, and when I give it what I think it wants that still doesn't work and also doesn't compile.例如“确保sqlite db存在,然后创建开放模式”等。我刚刚切换到ESLint,我无法弄清楚它想要什么语法,当我给它我认为它想要的东西仍然不起作用并且也不编译。 If I do the obvious thing and switch version2 to use Promise<boolean> then compilation fails with: src/dimTest.ts(23,13): error TS2322: Type 'void' is not assignable to type 'boolean'如果我做了显而易见的事情并将 version2 切换为使用Promise<boolean>然后编译失败: src/dimTest.ts(23,13): error TS2322: Type 'void' is not assignable to type 'boolean'

Am I misunderstanding something about promises and just getting lucky that it works or is this an ESLint issue?我是不是对 promise 有误解,只是很幸运它有效,还是 ESLint 问题? Thanks!谢谢!

/* eslint-disable no-console */
class test {
    constructor() {
        // ;
    }

    // this is what I originally used
    async version1():Promise<boolean> {
        return new Promise(async (resolve,reject) => {
            await this.delay(2000);
            await this.delay(500);
            await this.delay(250);
            resolve(true)
        });
    }

    // This method compiles but eslint is still says misused promise
    async version2(): Promise<boolean> {
        return new Promise(async (resolve, reject):Promise<any> => {
            await this.delay(2000);
            await this.delay(500);
            await this.delay(250);
            resolve(true);
        });
    }
    public async main(): Promise<void> {
        console.log("Hello!");
        await this.version1();
        console.log("Goodbye!");
        console.log("Hello!");
        await this.version2();
        console.log("Goodbye!");
    }

    protected delay(ms: number = 5000 ): Promise<number> {
        return new Promise((resolve) => { setTimeout(resolve, ms); });
    }
}
export { test };

const dt = new test();
void dt.main();

If the function is async then no need to return a promise,如果函数是async则无需返回承诺,

async version1(): Promise<boolean> {
        await this.delay(2000);
        await this.delay(500);
        await this.delay(250);
        return true;
    }

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

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