Skip to content

codemods for refactoring callbacks based functions to async-await syntax ๐Ÿš€

License

Notifications You must be signed in to change notification settings

harsilspatel/promise-me-callback

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

47 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

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

About

codemods for refactoring callbacks based functions to async-await syntax ๐Ÿš€

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published