codemods for refactoring callbacks based functions to async-await syntax 🚀
npm install -g jscodeshift
jscodeshift -t ./path/to/transformer.js ./path/to/js/source/**/*.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);
}
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;
}
squareRoot(magicNumber, (error, magicNumberSquareRoot) => {
if (err) {
// ignoring error 'cause yolo
}
console.log(`Square root of ${magicNumber} is ${magicNumberSquareRoot}`)
})
try {
let magicNumberSquareRoot = await squareRoot(magicNumber);
console.log(`Square root of ${magicNumber} is ${magicNumberSquareRoot}`)
} catch (error) {
// ignoring error 'cause yolo
};