Sitelet https://web.archive.org/web/20201113042759/https://github.com/meteor/meteor/issues/11120
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

Meteor.wrapAsync handling undefined properties wrong #11120

Open
nytamin opened this issue Jul 13, 2020 · 4 comments
Open

Meteor.wrapAsync handling undefined properties wrong #11120

nytamin opened this issue Jul 13, 2020 · 4 comments

Comments

@nytamin
Copy link

@nytamin nytamin commented Jul 13, 2020

I have come across an issue where Meteor.wrapAsync is not handling undefined properties as it should.

The documentation states that:

Meteor.wrapAsync(func, [context])
Wrap a function that takes a callback function as its final parameter.

But in the case of

const myWrapFunction = Meteor.wrapAsync((...args) => {
  console.log('arguments received in wrapped function:', args)

  const callback = args[args.length - 1] // Last argument
  if (typeof callback !== 'function') {
    throw new Meteor.Error(500, `Callback function is not a function!`)
  }
  callback() // to return
})
myWrapFunction(1, undefined) // does not throw, by pure luck
myWrapFunction(1, undefined, undefined) // throws "Callback function is not a function!"

The arguments provided into myWrapFunction is

myWrapFunction(1, undefined) // gives args = [1, Function] 
myWrapFunction(1, undefined, undefined) // gives args = [1, Function, undefined]

altough I expected this to happen:

myWrapFunction(1, undefined) // should give args = [1, undefined, Function] 
myWrapFunction(1, undefined, undefined) // should give args = [1, undefined, undefined, Function]

This creates a significant problem, since the callback might not be the last parameter - which is in direct conflict with the docs.
This means that myWrapFunction(1, undefined) and myWrapFunction(1) might produce different results, even though they are in general thought of as identical.

This has been addressed previously in #6816 , but I think it should be brought up again for review.

I see two possible solutions:

  1. Fix the bug (but that might be breaking and have significant impact)
  2. Change the documentation to instead say "Wrap a function that takes a callback function as its last (non-undefined) parameter".

Tested in Meteor 1.10.2
Repo for reproducing the issue: https://github.com/nytamin/meteor-wrapAsync-bug

@filipenevola filipenevola added this to the Release 1.11 milestone Aug 10, 2020
@stale
Copy link

@stale stale bot commented Sep 11, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale-bot label Sep 11, 2020
@stale
Copy link

@stale stale bot commented Sep 20, 2020

This issue has been automatically closed it has not had recent activity.

@stale stale bot closed this Sep 20, 2020
@filipenevola
Copy link
Member

@filipenevola filipenevola commented Nov 10, 2020

Hi, I believe we should update the docs, do you agree @nytamin ?

Change the documentation to instead say "Wrap a function that takes a callback function as its last (non-undefined) parameter".

Could you do that?

@filipenevola filipenevola reopened this Nov 10, 2020
@filipenevola filipenevola removed this from the Release vNext milestone Nov 10, 2020
@nytamin
Copy link
Author

@nytamin nytamin commented Nov 11, 2020

Sure, I can look into it. It'll be a good exercise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.