Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement macro with async function inside? #62

Closed
stereobooster opened this issue May 7, 2018 · 8 comments
Closed

How to implement macro with async function inside? #62

stereobooster opened this issue May 7, 2018 · 8 comments

Comments

@stereobooster
Copy link
Contributor

  • babel-plugin-macros version: 2.1.0
  • node version: 8.6
  • npm (or yarn) version: yarn 1.5.1

Relevant code or config

What you did:

What happened:

Reproduction repository:

https://github.com/stereobooster/lqip.macro

Problem description:

I want to implement lqip.macro. I'm not sure how to use async lqip in babel-plugin-macros, which seems to be expecting synchronous code. Am I wrong here? What is the way to work with async functions? Thanks

Suggested solution:

@kentcdodds
Copy link
Owner

Yes, this is a sad limitation to babel transforms. They are completely synchronous. @threepointone told me this crazy idea to make async node code run synchronously. It's going to be slow, but it works like a charm. Here's an example from real code that I'm using at PayPal:

const results = crossSpawn.sync('node', [
  '-e',
  `require("${spundlePath}")("${localesDir}", "${country}", "${locale}", (err, out) => {console.log(JSON.stringify(out))})`,
])
const en = JSON.parse(results.stdout)['en-US']

That should give you an idea of how to make it run synchronously. There may be a module for this.

In any case, I think we should either create a utility and expose that from babel-plugin-macros, or (probably better) create a module that makes this easier. What do you think?

@stereobooster
Copy link
Contributor Author

Can we make babel to accept async function? I'm not aware of implementation details. I will try your code meantime.

@kentcdodds
Copy link
Owner

Unfortunately not. But @hzoo may have some other ideas.

@stereobooster
Copy link
Contributor Author

I did this:

function syncLqip(path) {
  return execSync("node src/lqip.js " + path, { shell: false })
    .toString("utf8")
    .trim();
}

@stereobooster
Copy link
Contributor Author

I found workaround for my case. Should we close this ticket or keep open to find general solution?

@kentcdodds
Copy link
Owner

Maybe we could add a link to this issue in the author.md docs file?

@Zemnmez
Copy link

Zemnmez commented Apr 6, 2020

I've made @kentcdodds solution into a more generic & cleaner micro library: https://github.com/Zemnmez/do-sync

@kentcdodds
Copy link
Owner

Very cool @Zemnmez! Thank you 💯

kentcdodds added a commit that referenced this issue Apr 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants