0

I use this code to load .eslintrc:

const eslintrcs = {};
async function getLintOptions(path) {
    if (!eslintrcs[path]) {
        let eslintrc;
        try {
            eslintrc = await readFile('./.eslintrc');
            eslintrc = await readFile(`${path}/.eslintrc}`);
        } catch (err) {
            eslintrc = eslintrc || {};
        }
        eslintrcs[path] = JSON.parse(eslintrc);
        console.log(eslintrcs[path]);
    }

    return eslintrcs[path];
}

So I'm linting my files like this:

        files.forEach(async (filePath) => {
            try {
                let code;
                if (!options.skipLint) {
                    logFileProgress('Linting', filePath);
                    code = await readFile(filePath);
                    const messages = linter.verify(code, await getLintOptions(path));

The problem here is that as the code is asynchronous, so it stops after first await is found and keeps with sync code and the result is this one:

info: 2016-10-05 18:47 - Linting: src/server/_config.js
info: 2016-10-05 18:47 - Linting: src/server/admin/router.js
info: 2016-10-05 18:47 - Linting: src/server/server.js
...
{ parser: 'babel-eslint',
  extends: 'airbnb',
  rules: { indent: [ 2, 4, [Object] ] } }
{ parser: 'babel-eslint',
  extends: 'airbnb',
  rules: { indent: [ 2, 4, [Object] ] } }
{ parser: 'babel-eslint',
  extends: 'airbnb',
  rules: { indent: [ 2, 4, [Object] ] } }

Is there a way to stop an async function until it gets the result?

I've even tried to move the getLintOptions outside forEach loop:

        const eslintrc = await getLintOptions(path);
        files.forEach(async (filePath) => {
            try {
                let code;
                if (!options.skipLint) {
                    logFileProgress('Linting', filePath);
                    code = await readFile(filePath);
                    const messages = linter.verify(code, eslintrc);
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
David
  • 2,741
  • 16
  • 26

0 Answers0