Skip to content

Implement transform-async-to-promises - #7076

Open
MatAtBread wants to merge 24 commits into
babel:masterfrom
MatAtBread:master
Open

MatAtBread wants to merge 24 commits into
babel:masterfrom
MatAtBread:master

Conversation

@MatAtBread

Copy link
Copy Markdown

No description provided.

@MatAtBread MatAtBread changed the title Pull from babel/babel:master Implement transform-async-to-promises Dec 20, 2017
@babel-bot

babel-bot commented Dec 20, 2017 •

Copy link
Copy Markdown
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/7756/

@hzoo

hzoo commented Dec 20, 2017

Copy link
Copy Markdown
Member

Great thanks for the initial PR, are you ok if I make some commits? I think this has tests from the other transforms, so we should merge into the existing plugin (which I can do if it was erroring) or at least remove the old files.

@MatAtBread

Copy link
Copy Markdown
Author

@hzoo - as requested, pull implementing transform-async-to-promises.

As I understand it, you're intending to merge into transform-async-to-generator and used options to specify the target.

I have implemented only two options:

  • runtime is the (optional) named identifier that contains the nodent-runtime. The default is no runtime. I included this option as the generated code is considerably smaller and somewhat faster in a lot of engines as without the runtime as lot of nested try..catch blocks get generated. If used, the included runtime is newly crafted 'nodent-runime/promises' (https://github.com/MatAtBread/nodent-runtime/blob/master/promise.js) that is both very small (no implementation of Promises or the generator runtime) and no longer pollutes Function.prototype. This updated [email protected] has already been released to npmjs.

  • codeGenerationOptions provides a means to set alternative defaults for symbol names, etc.

@MatAtBread

Copy link
Copy Markdown
Author

...sure - it's your project/plugin. Like everyone I have an opinion, but I'm no Babel expert (and only a very occasional user). Feel free to ask questions, raise issues, etc. I'm certainly interested in where it goes next

ljharb
ljharb previously requested changes Dec 21, 2017
}).program;
}

/* Polyfill for Object.assign(...) from:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not use MDN code in production anywhere :-/ this code doesn’t work with symbols, and has a number of other subtle bugs.

I’d suggest object.assign on npm.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well we don't need this at all, can just use Object.assign because min version is Node 4, or object spread

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look closely, it's not polyfilling the prototype, it's just defined & called locally. In fact, it could easily be replaced by a simple for-loop. And as hzoo says, if you're only targeting Node >4, Object.assign() will work fine.

@hzoo - can I assume you're OK to maintain this for now?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Babel 7 does target Node 4 so you can use Object.assign by itself in this PR

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MatAtBread right, i'm saying that basically all the code on MDN is broken and should never be used for any reason beyond "loosely explaining what a thing does". Since babel requires node 4+, this can just be deleted, but the larger point about MDN code stands.

}).program;
}

/* Polyfill for Object.assign(...) from:

@ljharb ljharb Dec 21, 2017 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my above comment; this shouldn’t be here.


if (options.codeGenerationOptions) {
let keys = Object.keys(options.codeGenerationOptions);
for (let i=0; i<keys.length;i++) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

per previous comments, this can just be replaced with Object.assign(opts, options.codeGenerationOptions);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(IIRC we use transform-object-rest-spread in babel itself so) alternatively,

const opts = {
  // Code generation options
  es6target: false,
  // ...
  $makeThenable: "$makeThenable",
  ...options.codeGenerationOptions
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look at the code, there's a conditional to prevent unknown keys being placed in the destination object. This is not the same as replacing it with Object.assign, which unconditionally copies all keys, irrespective of their existence in the destination.

I'm not familiar enough with the semantics of the Object spread operator to know if it is a suitable replacement for this simple loop. Is there something about for loops you don't like?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just the unreadability, hence this very confusion :-)

if (options.codeGenerationOptions) {
let keys = Object.keys(options.codeGenerationOptions);
for (let i=0; i<keys.length;i++) {
if (keys[i] in opts)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line seems to be indented weirdly?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea why - I'd guess something put tabs in place of spaces or vice versa. I'm not able to fix this at the moment (away from my home setup with the correct tools). Feel free to edit to match your preferred style.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...I just had a go with the github editor, but TBH I'm not familiar with how these are best merged online.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you're ok to accept the PR?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, sorry, I'll dismiss my review. I'm not personally confident in approving this, but I definitely am not blocking it further :-)

@matAtWork

Copy link
Copy Markdown

@hzoo - I have no idea how to finally get this pull accepted. Maybe you can help?

return generate(ast, {}).code;
}

function parse(code) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondering why you need to parse code here? Isn't it a regular Babel plugin?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an internal templating system which needs parse the templates.


if (runtime) {
newAst.body.splice(0, 0, {
type: "ImportDeclaration",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please use babel-types for that? and maybe babel-traverse to place to node at the correct location?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I don't have the bandwidth to learn a new API or style/code conventions. I started this as a favour to @hzoo, and as far as I know the current implementation works.

@hzoo - I'm happy to invite you (or anyone else) to my babel fork, but I'm really unable to provide the time to do this to the Babel team's satisfaction.

}).program;
}

const transform = require("nodent-transform").transform;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not without a lot of effort - far more than I can commit to. This was discussed initially in MatAtBread/fast-async#46 and at that point the costs of re-coding for the babel API (ignoring the stylistic requirements) would not be viable without the significant contribution of knowledgable babel contributors, hence a half-way where the 'transform' part of nodent was stripped of its dependancies was provided by me.

@xtuc xtuc added the PR: New Feature 🚀 A type of pull request used for our changelog categories label Jan 10, 2018
Fix indenting lines 44-51
@matAtWork

matAtWork commented Jan 19, 2018 •

Copy link
Copy Markdown

Is anyone able to explain the CircleCI issues? They appear unrelated to the changes in the pull
Thanks.

@erykpiast

Copy link
Copy Markdown

Hello @mAAdhaTTah and Babel Team (@nicolo-ribaudo, @hzoo)! Do you plan to continue work on this PR? What are the pending issues? Do you still see a value in this concept? If you need any help, I'm here for you!

@MatAtBread

Copy link
Copy Markdown
Author

I have no plans to a work on it.

It was left to @hzoo and the Babel team to decide whether to incorporate it or not

@nicolo-ribaudo

nicolo-ribaudo commented Mar 1, 2020 •

Copy link
Copy Markdown
Member

#7076 (review) is a good summary of the minimum steps that need to be done to move this forward. However,

  1. This PR is meant to avoid depending on regenerator-runtime when using async functions. If you configure @babel/preset-env to target >0.6%, not ie 11 it will not use regenerator, and if you lower your targets (>0.5% or accept ie 11) regenerator is included for browsers that don't support promises anyway.
  2. If this doesn't get accepted into this monorepo, nothing prevents anyone from releasing it as a third-party plugin, and use it disabling transform-regenerator and async-to-generator in @babel/preset-env's options.

@curiousdannii

Copy link
Copy Markdown

In the time that this PR has sat open the need for it has largely disappeared. Async function support is now up to above 90%. Still, thank you @MatAtBread for trying.

@ljharb

ljharb commented Mar 2, 2020 •

Copy link
Copy Markdown
Member

I don’t agree that the need has disappeared at all; I’d still very much like to see this land in core.

“90%” doesn’t mean anything; since everyone’s targets are different.

@erykpiast

Copy link
Copy Markdown

There is no need to remove the current approach when this one would be merged. It may be an option like a loose mode is for many presets, don't you think? In that way, everybody may choose what is best for a particular use case.

@nicolo-ribaudo

Copy link
Copy Markdown
Member

This transform really needs to be rewritten as a Babel plugin, as regenerator-transform has been for a long time (though not originally!), so that it can benefit from all the pipelining and NodePath/Scope-reuse optimizations that Babel's plugin system provides, and so that the Babel team can meaningfully maintain it, as you are implicitly asking them to do. Right now this plugin has a bus/vacation factor of 1, and that becomes a liability for Babel when it is included in core.

#7076 (review)

@Dan503

Dan503 commented Mar 2, 2020

Copy link
Copy Markdown

What would the use case be for preferring generators over promises though? 😕

@erykpiast

Copy link
Copy Markdown

@nicolo-ribaudo I meant an option to the preset, similar to modules in env (as far as I understand it's a case of adding a plugin conditionally).

@Dan503 I suppose compliance with specs would be the benefit of the original generator approach. Maybe there is something more, like handling more complex cases. I'm thinking about nested and conditional awaits, async generators etc.

In general, what I would like to accomplish is not having to advise people dropping async-await syntax for simple cases, like unconditional series of asynchronous operations. I don't like multiple then callbacks like everybody else, but what I don't like even more is that whole regenerator runtime and a lot of boilerplate added to the bundle just to handle the very simple cases. I also understand the necessity of having a fallback solution for more complex code, constructs that we may even not dream about. That's why I see a space for two separate and independent solutions, possibly chosen by a preset option.

@nicolo-ribaudo

nicolo-ribaudo commented Mar 2, 2020 •

Copy link
Copy Markdown
Member

I meant an option to the preset, similar to modules in env (as far as I understand it's a case of adding a plugin conditionally).

It doesn't matter if it's the default behavior or not. If it's not implemented as a Babel plugin we cannot maintain it. Nodent isn't a Babel plugin. It passes the original AST to an external compiler, which doesn't use Babel's architecture.

Deleted - I was wrong 1. When there is an async function, transform it into a source code string 2. Pass this string to the nodent compiler 3. Get a compiled source code from the node compiler, parse it and put it back in the original position

The whole compilation is opaque to us, and it's not something that we can maintain.
The current implementation (as a "fake" plugin) can be published to npm, and then you can do:

{
  "presets": [
    ["@babel/preset-env", {
      "exclude": ["async-to-generator"]
    }]
  },
  "plugins": [
    "transform-async-with-nodent"
  ]
}

We have nothing against transpiling async to promises, but also the community is free to publish a plugin outside of preset-env.

I suppose compliance with specs would be the benefit of the original generator approach. Maybe there is something more, like handling more complex cases. I'm thinking about nested and conditional awaits, async generators etc.

@Dan503 This. 💯

Also, transpiling async functions to generators (not to es5) produces an output which is much more similar to the original one rather than when using promises:

// input
(async () => {
  const response = await fetch(url);
  const asJSON = await response.json();
  
  console.log(asJSON);
})();

// Compiled to generators
_asyncToGenerator(function* () {
  const response = yield fetch(url);
  const asJSON = yield response.json();
  console.log(asJSON);
})();

// Compiled to promises
(() => new Promise(function ($return, $error) {
    let response, asJSON;
    return fetch(url).then((function ($await_1) {
        response = $await_1;
        return response.json().then((function ($await_2) {
            asJSON = $await_2;
            console.log(asJSON);
            return $return();
        }).$asyncbind(this, $error), $error);
    }).$asyncbind(this, $error), $error);
}))();

Additionally, I just noticed that nodent's output modifiers the native Function.prototype object. It's not wrong per se, but it's something that so far we have been avoided on purpouse in Babel's generated code.

@MatAtBread

Copy link
Copy Markdown
Author

Actually, that's not true. It passes the parsed AST to nodent, which does all the tree transforms itself (not using the Babel utils).

I'm all in favour of debate, but please base it on facts. It did considerable work on factoring it the transformer especially for this PR. It's up to you if you use it, but don't misrepresent what it does

@nicolo-ribaudo

Copy link
Copy Markdown
Member

Thanks, I updated my comment.

@nicolo-ribaudo

Copy link
Copy Markdown
Member

In the short term, we could link the third-party plugin in Babel's docs.

@erykpiast

erykpiast commented Mar 2, 2020 •

Copy link
Copy Markdown

Alright, I see the whole story now. If it's about maintainability and code ownership, maybe there is nothing wrong with rejecting this PR and continuing the work somewhere else? I suppose rewriting the code to Babel utils would require starting the work from scratch. It's far more than just polishing the code.

If we're focused on results more than on resolving technical or operational issues, maybe we should start from releasing a stable version of fast-async working well with Babel@7, as a community plugin. Then we could try to measure its adaptation in the ecosystem and battle test in real-world projects. I'm not sure how wide is the usage of the plugin now, but NPM shows rather low numbers. Maybe refreshing the project and some marketing in social media would be enough to raise its popularity and get more feedback. The correct plugin is this one.

@nicolo-ribaudo, @MatAtBread, what do you think?

@matAtWork

Copy link
Copy Markdown

Whatever is appropriate for you.

As for maintainability, it is a complex piece of code, and quite old (the original implementation is maybe 5 years old). However, it has a full test suite in the main compiler (nodent, as opposed no nodent-transform which is just the AST transformer) which covers many more test cases than babel's current set and it has been in use in production systems for many years. I've not had to do anything to it for months to meet edge cases or indeed the spec (there are compiler flags for strict and loose adherence to the spec, along with options for output to ES5, Promises, ES6, etc).

If I remember correctly, the babel version defaults to strict adherence to the spec with no runtime at all and using Promises.

All that having been said, like last time this came up, this seems to be a "political" or "philosophical" debate. It's not about the code or it's functionality (which is fully in line with the rest of the world), it's about whether making the option available to end users of babel should be easy (included) or hard (a comment somewhere in the docs) vs. the potential cost of maintenance. This is not something I have a view on as I'm not a babel maintainer.

@erykpiast

Copy link
Copy Markdown

Matt, you're confusing me. Are you at work or having a sandwich break? 😐

@MatAtBread

Copy link
Copy Markdown
Author

Erm, both? It was lunch time in London.

I'm not trying to be confusing. I'm genuinely agnostic about whether Babel include the transformer or not. Most of my day job is backend, or if it's FE it's not for IE11, and I'm not a Babel user in general. So it really is whether there's a demand in the Babel user base for an async transformer with no runtime.

As an aside, when I first wrote it, it really was 10x quicker than regenerator & Promise polyfills, but now these are both implemented in major engines like V8, the point is moot as native is easily twice as quick as any userland implementations

Should you decide to integrate into Babel's core product, I'm happy to advise where I can, but it's not something I have an active interest in myself

@MatAtBread

Copy link
Copy Markdown
Author

I also think you're looking at the wrong plugin. Try https://www.npmjs.com/package/fast-async - that has 22k a week. I think someone built their own plugin on top of fast-sync for Babel & used the name

@erykpiast

erykpiast commented Mar 2, 2020 •

Copy link
Copy Markdown

Just a stupid joke, you use two GitHub accounts interchangeably. I suppose by accident, but I found it funny because of the names. Sorry about that.

Do you refer runtime speed, right? I'm more interested in optimizing bundle size. I didn't consider potential improvement or regression in execution, but my gut feeling is Promises winning here as well, there is less code to run. On the other hand, there is some cost of creating many lambdas for then callbacks... It's definitely something necessary to check out.

Huh, I'm looking at the wrong plugin! 😱

@MatAtBread

Copy link
Copy Markdown
Author

Oh yeah! I forgot. I have a paid-for-put-food-on-the-table and other stuff. It depends if I respond from my phone or desktop!

@dpraul

dpraul commented Mar 2, 2020

Copy link
Copy Markdown

since this is showing activity again, just chiming in once more: if you're just trying to avoid using regenerator-runtime for promises, we've been using fast-async in production for a long time now with no issue.

babel.config.js
module.exports = {
  plugins: [
    [
      '@babel/plugin-transform-regenerator',
      {
        asyncGenerators: false,
        generators: true,
        async: false,
      },
    ],
    [
      'module:fast-async',
      {
        spec: true,
      },
    ],
  ],
  presets: [
    [
      '@babel/preset-env',
      {
        exclude: [
          'transform-regenerator',
          'transform-async-to-generator',
        ],
      },
    ],
  ],
};

@matAtWork

Copy link
Copy Markdown

@erykpiast - if you want the Babel-7 plugin, that I prepped for this PR, it's here: https://github.com/MatAtBread/fast-async/tree/babel-v7

@matAtWork

Copy link
Copy Markdown

@nicolo-ribaudo The comment about modifying the Function.prototype is a fair one, but you have not used the super-clean & spec options to test. Here's is the output with no runtime at all (function protoypical or otherwise).

Of course, it's even bigger and more ugly, but somewhat easier to read that regenerators output. Of course, if you have a runtime that does generators, you don't need either.

http://nodent.mailed.me.uk/#(async%20()%20%3D%3E%20%7B%0A%20%20const%20response%20%3D%20await%20fetch(url)%3B%0A%20%20const%20asJSON%20%3D%20await%20response.json()%3B%0A%20%20%0A%20%20console.log(asJSON)%3B%0A%7D)()%3B%0A~options~%7B%22mode%22%3A%22promises%22%2C%22promiseType%22%3A%22Zousan%22%2C%22noRuntime%22%3Atrue%2C%22es6target%22%3Atrue%2C%22wrapAwait%22%3Atrue%2C%22spec%22%3Afalse%7D

@coolgod

coolgod commented Jun 4, 2020

Copy link
Copy Markdown

What's the latest story about this PR? Seems it has been open for a long time.

@nicolo-ribaudo

Copy link
Copy Markdown
Member

You can use the async-to-promises plugin as shown in the config example at #7076 (comment)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: New Feature 🚀 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.