Skip to content

Files

Latest commit

 

History

History
33 lines (26 loc) · 765 Bytes

no-promise-in-callback.md

File metadata and controls

33 lines (26 loc) · 765 Bytes

Pattern: Promise in error-first callback

Issue: -

Description

Mixing Promises within error-first callback functions makes the code harder to follow and complicates error handling. Keep async code consistent by using either callbacks or Promises/async-await throughout.

Examples

Example of incorrect code:

readFile('file.txt', (err, data) => {
  if (err) handleError(err);
  else {
    Promise.resolve(data)
      .then(processData);
  }
});

Example of correct code:

// Using promises consistently
const data = await readFile('file.txt');
await processData(data);

// Or using callbacks consistently
readFile('file.txt', (err, data) => {
  if (err) handleError(err);
  else processData(data, handleResult);
});