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

Arguments undefined when spreading args in async arrow function (T7329) #4270

Closed
babel-bot opened this issue May 2, 2016 · 5 comments
Closed
Labels
i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@babel-bot
Copy link
Collaborator

Issue originally made by @Lapixx

Bug information

  • Babel version: 6.7.7
  • Node version: 5.3.0
  • npm version: 3.3.12

Options

ES2015 and Stage 0 preset.

Input code

// modified from kadirahq/graphql-errors

const modifyResolver = (field, handler) => {

  const originalResolve = field.resolve;

  field.resolve = async (...args) => { // Workaround 1: use `async function(...args) {`

    try {
      // const b = args; // Workaround 2: Access args -- even when unused.
      const res = originalResolve(...args); // Problem: arguments is undefined here
      return await Promise.resolve(res);
    }
    catch (err) {
      throw handler(err);
    }
  };
};

Description

Basically, when I try to define a function like so:

const myFunc = async (...args) => {
    otherFunc(...args);
};

The arguments are undefined. When you look at the output you can clearly see that arguments gets assigned outside of the function scope. I'm not sure why that happens. Removing async fixes the problem, using the function keyword fixes the problem, and (my favourite), assigning args to a value (or otherwise accessing it) also fixes the problem.

[[ https://babeljs.io/repl/#?evaluate=true&lineWrap=false&presets=es2015%2Creact%2Cstage-0&experimental=true&loose=false&spec=false&code=%2F%2F%20modified%20from%20kadirahq%2Fgraphql-errors%0A%0Aconst%20modifyResolver%20%3D%20(field%2C%20handler)%20%3D%3E%20%7B%0A%20%20%0A%20%20const%20originalResolve%20%3D%20field.resolve%3B%0A%20%20%0A%20%20field.resolve%20%3D%20async%20(...args)%20%3D%3E%20%7B%20%2F%2F%20Workaround%201%3A%20use%20%60async%20function(...args)%20%7B%60%0A%0A%20%20%20%20try%20%7B%0A%20%20%20%20%20%20%2F%2F%20const%20b%20%3D%20args%3B%20%2F%2F%20Workaround%202%3A%20Access%20args%20--%20even%20when%20unused.%0A%20%20%20%20%20%20const%20res%20%3D%20originalResolve(...args)%3B%20%2F%2F%20Problem%3A%20arguments%20is%20undefined%20here%0A%20%20%20%20%20%20return%20await%20Promise.resolve(res)%3B%0A%20%20%20%20%7D%0A%20%20%20%20catch%20(err)%20%7B%0A%20%20%20%20%20%20throw%20handler(err)%3B%0A%20%20%20%20%7D%0A%20%20%7D%3B%0A%7D%3B | REPL with sample code ]]

@danharper
Copy link
Member

danharper commented Oct 16, 2016

Still an issue. Minimal input:

const modifyResolver = () => {
  return async (...args) => {
    //args;
    y(...args);
  };
};

Output:

"use strict";

var _arguments = arguments;

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

var modifyResolver = function modifyResolver() {
  return function () {
    var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee() {
      var _args = _arguments;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              //args;
              y.apply(undefined, _args);

            case 1:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, undefined);
    }));

    return function (_x) {
      return _ref.apply(this, arguments);
    };
  }();
};

(args -> _args references arguments from the outer scope).


Seems related to #4219?

@resistdesign
Copy link

@gajus Ah, you're right! Good call!

@aaronang
Copy link
Member

Closing this issue because it is a duplicate of #4219. Thank you for taking the time to report though 😬

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label May 5, 2018
@lock lock bot locked as resolved and limited conversation to collaborators May 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

5 participants