codemodsquad / asyncify Goto Github PK
View Code? Open in Web Editor NEWDon't keep your promises ๐
License: MIT License
Don't keep your promises ๐
License: MIT License
async function createUser(args) {
const {username, groups} = args
const user = await Users.create({username})
.then(user => {
if (groups) {
return addUserToGroups(user, groups)
} else if (foo) {
console.log('test')
} else if (bar) {
console.log('test2')
}
console.log('blah')
return user
})
.then(user => {
if (groups) {
console.log('test')
} else {
return 'noGroups'
}
return 'user'
})
.catch(err => {
console.error(err.stack)
return dummyUser()
})
}
async function createUser(args) {
const {username, groups} = args
let user
try {
const user1 = await Users.create({ username })
const user0 = await (async user => {
if (groups) {
return addUserToGroups(user, groups)
} else if (foo) {
console.log('test')
} else if (bar) {
console.log('test2')
}
console.log('blah')
return user
})(user1)
if (groups) {
console.log('test')
user = 'user'
} else {
user = 'noGroups'
}
} catch (err) {
console.error(err.stack)
user = await dummyUser()
}
}
async function createUser(args) {
const {username, groups} = args
let user
try {
let user0
const user1 = await Users.create({ username })
if (groups) {
user0 = await addUserToGroups(user1, groups)
} else if (foo) {
console.log('test')
} else if (bar) {
console.log('test2')
} else {
console.log('blah')
user0 = await user1
}
if (groups) {
console.log('test')
user = 'user'
} else {
user = 'noGroups'
}
} catch (err) {
console.error(err.stack)
user = await dummyUser()
}
}
`
Again, thanks for this code mod. It works very well so far!
I just wanted to pass on some examples where comments in the transformed code were not placed like I expected them to be.
@codemodsquad/asyncify
: 2.0.5jscodeshift
: 0.11.0
function a (x) {
// Apply f to x because ...
return f(x)
.then(() => x);
}
function b (x) {
return f(x)
// Always return true because ...
.then(() => true);
}
async function a(x) {
await f(x);
// Apply f to x because ...
return x;
}
async function b(x) {
await // Always return true because ...
f(x);
return true;
}
async function a(x) {
// Apply f to x because ...
await f(x);
return x;
}
async function b(x) {
await f(x);
// Always return true because ...
return true;
}
Cases like
const promises = Promise.all([
user.update().then(() => user.reload()),
post.update().then(() => post.reload()),
])
Would be better left alone since they become messy when converted to IIAAFs
Following the straight-forward instructions in the README:
git clone https://github.com/codemodsquad/asyncify
npx jscodeshift -t asyncify/index.js path/to/your/project/**/*.js
It seems there is a step missing. There is no index.js
in the root of the project, and running yarn build
fails with
> babel src --out-dir es --extensions ".ts" --source-maps inline && cross-env BABEL_ENV=es5 babel src --out-dir . --extensions ".ts"
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /<snip>/asyncify/node_modules/@babel/preset-env/node_modules/@babel/helper-compilation-targets/package.json
Is there a missing step? I looked at the circleci job and there don't seem to be any special env vars or other configurations in place, but there also aren't any artifacts published.
I forgot about this important detail of .then
:
If it is not a function, it is internally replaced with an "Identity" function (it returns the received argument).
Right now asyncify always replaces A.then(B)
with await B(await A)
if B isn't a function expression. I need to walk the scope to determine if it's a function expression or not, and if it's impossible to tell, replace with a runtime check and developer warning.
(note to self)
e.g. the following currently ends up creating instances0
etc
.then(instances => {
if (options.runHooks) {
return this.runHooks().then(() => instances)
}
return instances
})
.then(instances => {
...
})
First of all thanks for the great work! This code mod seems to do a fantastic job from what I saw up until now.
Unfortunately there was a problem with one particular source file. To reproduce:
git clone https://github.com/apache/cordova-android.git
cd cordova-android
npm i jscodeshift @codemodsquad/asyncify
npx jscodeshift --verbose=2 -t node_modules/@codemodsquad/asyncify/index.js bin/templates/cordova/lib/check_reqs.js
There is no output from the process and it stays at 100% CPU utilization indefinitely (well, after 20 min or so I killed it).
Failing test case:
export const input = `
it('should support ordering with only belongsTo includes', function() {
const User = this.sequelize.define('User', {}),
Item = this.sequelize.define('Item', { 'test': DataTypes.STRING }),
Order = this.sequelize.define('Order', { 'position': DataTypes.INTEGER });
User.belongsTo(Item, { 'as': 'itemA', foreignKey: 'itemA_id' });
User.belongsTo(Item, { 'as': 'itemB', foreignKey: 'itemB_id' });
User.belongsTo(Order);
return this.sequelize.sync().then(() => {
return promiseProps({
users: User.bulkCreate([{}, {}, {}]).then(() => {
return User.findAll();
}),
items: Item.bulkCreate([
{ 'test': 'abc' },
{ 'test': 'def' },
{ 'test': 'ghi' },
{ 'test': 'jkl' }
]).then(() => {
return Item.findAll({ order: ['id'] });
}),
orders: Order.bulkCreate([
{ 'position': 2 },
{ 'position': 3 },
{ 'position': 1 }
]).then(() => {
return Order.findAll({ order: ['id'] });
})
}).then(results => {
const user1 = results.users[0];
const user2 = results.users[1];
const user3 = results.users[2];
const item1 = results.items[0];
const item2 = results.items[1];
const item3 = results.items[2];
const item4 = results.items[3];
const order1 = results.orders[0];
const order2 = results.orders[1];
const order3 = results.orders[2];
return Promise.all([
user1.setItemA(item1),
user1.setItemB(item2),
user1.setOrder(order3),
user2.setItemA(item3),
user2.setItemB(item4),
user2.setOrder(order2),
user3.setItemA(item1),
user3.setItemB(item4),
user3.setOrder(order1)
]);
}).then(() => {
return User.findAll({
'include': [
{ 'model': Item, 'as': 'itemA', where: { test: 'abc' } },
{ 'model': Item, 'as': 'itemB' },
Order],
'order': [
[Order, 'position']
]
}).then(as => {
expect(as.length).to.eql(2);
expect(as[0].itemA.test).to.eql('abc');
expect(as[1].itemA.test).to.eql('abc');
expect(as[0].Order.position).to.eql(1);
expect(as[1].Order.position).to.eql(2);
});
});
});
});
`
export const options = {}
export const expected = `
it('should support ordering with only belongsTo includes', async function() {
const User = this.sequelize.define('User', {}),
Item = this.sequelize.define('Item', { 'test': DataTypes.STRING }),
Order = this.sequelize.define('Order', { 'position': DataTypes.INTEGER });
User.belongsTo(Item, { 'as': 'itemA', foreignKey: 'itemA_id' });
User.belongsTo(Item, { 'as': 'itemB', foreignKey: 'itemB_id' });
User.belongsTo(Order);
await this.sequelize.sync();
const results = await promiseProps({
users: User.bulkCreate([{}, {}, {}]).then(() => {
return User.findAll()
}),
items: Item.bulkCreate([
{ 'test': 'abc' },
{ 'test': 'def' },
{ 'test': 'ghi' },
{ 'test': 'jkl' }
]).then(() => {
return Item.findAll({ order: ['id'] })
}),
orders: Order.bulkCreate([
{ 'position': 2 },
{ 'position': 3 },
{ 'position': 1 }
]).then(() => {
return Order.findAll({ order: ['id'] });
})
});
const user1 = results.users[0];
const user2 = results.users[1];
const user3 = results.users[2];
const item1 = results.items[0];
const item2 = results.items[1];
const item3 = results.items[2];
const item4 = results.items[3];
const order1 = results.orders[0];
const order2 = results.orders[1];
const order3 = results.orders[2];
await Promise.all([
user1.setItemA(item1),
user1.setItemB(item2),
user1.setOrder(order3),
user2.setItemA(item3),
user2.setItemB(item4),
user2.setOrder(order2),
user3.setItemA(item1),
user3.setItemB(item4),
user3.setOrder(order1)
]);
const as = await User.findAll({
'include': [
{ 'model': Item, 'as': 'itemA', where: { test: 'abc' } },
{ 'model': Item, 'as': 'itemB' },
Order],
'order': [
[Order, 'position']
]
});
expect(as.length).to.eql(2);
expect(as[0].itemA.test).to.eql('abc');
expect(as[1].itemA.test).to.eql('abc');
expect(as[0].Order.position).to.eql(1);
expect(as[1].Order.position).to.eql(2);
});
`
It's always safe without conversion
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.