Remove `async` from `Promise`

JavaScript pattern

The Promise is already executed asynchronously and exceptions thrown by the function will be lost.

Creating an Promise from an async function is usually an error.


Apply with the Grit CLI
grit apply no_async_promise_executor

Remove async from Promise executor function if there is no await

BEFORE
const foo = new Promise(async (resolve, reject) => {
  readFile("foo.txt", function (err, result) {
    if (err) {
      reject(err);
    } else {
      resolve(result);
    }
  });
});
AFTER
const foo = new Promise((resolve, reject) => {
  readFile("foo.txt", function (err, result) {
    if (err) {
      reject(err);
    } else {
      resolve(result);
    }
  });
});

Transform Promise using async to async function

BEFORE
const foo = new Promise(async (resolve, reject) => {
  await readFile("foo.txt", function (err, result) {
    if (err) {
      reject(err);
    } else {
      resolve(result);
    }
  });
});
AFTER
const foo = (async () => {
  await readFile("foo.txt", function (err, result) {
    if (err) {
      throw err;
    } else {
      return result;
    }
  });
})();

Transform Promise to async/await function

BEFORE
const result = new Promise(async (resolve, reject) => {
  resolve(await foo);
});
AFTER
const result = (async () => {
  return await foo;
})();

Don't change Promise executor function

JAVASCRIPT
const foo2 = new Promise((resolve, reject) => {
  readFile("foo2.txt", function (err, result) {
    if (err) {
      reject(err);
    } else {
      resolve(result);
    }
  });
});

const result2 = Promise.resolve(foo2);