Skip to content

Latest commit

 

History

History
53 lines (49 loc) · 1.33 KB

README.md

File metadata and controls

53 lines (49 loc) · 1.33 KB

promise-me-callback

codemods for refactoring callbacks based functions to async-await syntax 🚀

Usage

npm install -g jscodeshift
jscodeshift -t ./path/to/transformer.js ./path/to/js/source/**/*.js

Refactoring callee

callee.js
const squareRoot = (x, callback) => {
  if ((x) < 0) {
    callback(new Error('MathDomainError: square root of a negative number does not exist'))
  }
  const sqRt = Math.sqrt(x); // call me a math whiz
  callback(null, sqRt);
}
callee.js transformed with remove-callback.js
const squareRoot = async x => {
  if ((x) < 0) {
    throw new Error('MathDomainError: square root of a negative number does not exist');
  }
  const sqRt = Math.sqrt(x); // call me a math whiz
  return sqRt;
}

Refactoring caller

caller.js
squareRoot(magicNumber, (error, magicNumberSquareRoot) => {
  if (err) {
    // ignoring error 'cause yolo
  }
  console.log(`Square root of ${magicNumber} is ${magicNumberSquareRoot}`)
})
caller.js transformed with await-function.js
try {
  let magicNumberSquareRoot = await squareRoot(magicNumber);
  console.log(`Square root of ${magicNumber} is ${magicNumberSquareRoot}`)
} catch (error) {
  // ignoring error 'cause yolo
};

License

MIT