Skip to content

Instantly share code, notes, and snippets.

@hex-ci
Forked from pgherveou/yield-to-async.js
Created July 23, 2021 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hex-ci/a70065f4acb0a65fdced8a45c7069e77 to your computer and use it in GitHub Desktop.
Save hex-ci/a70065f4acb0a65fdced8a45c7069e77 to your computer and use it in GitHub Desktop.
convert co/yield to to async/await
// https://astexplorer.net/#/gist/48d24982fceb3f258cc2dcd764fe8e38/937f48fc25443224a08bb20774a7d8868052770c
export default function ({types: t}) {
return {
visitor: {
ObjectMethod(path) {
if (path.node.generator) {
path.node.async = true
path.node.generator = false
}
},
ClassMethod(path) {
if (path.node.generator) {
path.node.async = true
path.node.generator = false
}
},
FunctionExpression(path) {
if (path.node.generator) {
path.node.async = true
path.node.generator = false
}
},
FunctionDeclaration(path) {
if (path.node.generator) {
path.node.async = true
path.node.generator = false
}
},
YieldExpression: function(path) {
let argument = path.node.argument
path.replaceWith(t.awaitExpression(argument), false)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment