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

Recursion within async functions #8783

Open
nathan3000 opened this Issue Sep 28, 2018 · 4 comments

Comments

Projects
None yet
4 participants
@nathan3000

nathan3000 commented Sep 28, 2018

Bug Report

Current Behavior
When trying to upgrade to Babel 7. Calling async function from within self-invoking function now causes ReferenceError: poll is not defined.

Input Code

(async function poll() {
  console.log(await Promise.resolve('Hello'))
  setTimeout(poll, 1000);
})();

Transformed Code

(function () {
  var _poll = _asyncToGenerator(
  /*#__PURE__*/
  regeneratorRuntime.mark(function _callee() {
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.t0 = console;
            _context.next = 3;
            return Promise.resolve('Hello');

          case 3:
            _context.t1 = _context.sent;

            _context.t0.log.call(_context.t0, _context.t1);

            setTimeout(poll, 1000);

          case 6:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

  return function poll() {
    return _poll.apply(this, arguments);
  };
})()();

Error

Applications.js:64 Uncaught (in promise) ReferenceError: poll is not defined
    at _callee$ (Applications.js:64)
    at tryCatch (runtime.js:62)
    at Generator.invoke [as _invoke] (runtime.js:296)
    at Generator.prototype.(anonymous function) [as next] (webpack:///./node_modules/regenerator-runtime/runtime.js?:114:21)
    at asyncGeneratorStep (Applications.js:19)
    at _next (Applications.js:21)

Expected behavior/code
Recursively calls async function.

Babel Configuration (babel.config.js, webpack.config.js)

module.exports = function (api) {
    api.cache(true)
    const presets = [ '@babel/preset-env', '@babel/preset-react' ];
    const plugins = [ '@babel/plugin-proposal-class-properties' ];
  
    return {
      presets,
      plugins
    };
  }

Using @babel/polyfill in webpack.config.js

entry: {
  react: ['@babel/polyfill', './index.js']
},

Environment

  • Babel version(s): v7.0.0
  • Node/npm version: v8.9.0
  • OS: OSX 10.12.6
  • Monorepo: no
  • How you are using Babel: loader

Additional context/Screenshots
screen shot 2018-09-28 at 12 33 51

@babel-bot

This comment has been minimized.

Show comment
Hide comment
@babel-bot

babel-bot Sep 28, 2018

Collaborator

Hey @nathan3000! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

Collaborator

babel-bot commented Sep 28, 2018

Hey @nathan3000! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@GriffinSauce

This comment has been minimized.

Show comment
Hide comment
@GriffinSauce

GriffinSauce Oct 3, 2018

@nathan3000 I ran into the same issue and found a temporary workaround:

async function poll() {
    setTimeout(repoll, 1000); // eslint-disable-line no-use-before-define
}

function repoll(...args) {
  return poll(...args);
}

YMMV but it might help until the actual problem is fixed.
(the return value and arguments are relevant to my context, maybe not for you)

GriffinSauce commented Oct 3, 2018

@nathan3000 I ran into the same issue and found a temporary workaround:

async function poll() {
    setTimeout(repoll, 1000); // eslint-disable-line no-use-before-define
}

function repoll(...args) {
  return poll(...args);
}

YMMV but it might help until the actual problem is fixed.
(the return value and arguments are relevant to my context, maybe not for you)

@nathan3000

This comment has been minimized.

Show comment
Hide comment
@nathan3000

nathan3000 Oct 8, 2018

Thanks @GriffinSauce. I'll go with that for now.

nathan3000 commented Oct 8, 2018

Thanks @GriffinSauce. I'll go with that for now.

@yairEO

This comment has been minimized.

Show comment
Hide comment
@yairEO

yairEO Oct 15, 2018

How did such a huge bug didn't fail a test?

This is a serious issue because it breaks a common practice

yairEO commented Oct 15, 2018

How did such a huge bug didn't fail a test?

This is a serious issue because it breaks a common practice

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