2

I'm sifting through some code and I don't get how this piece of code is able to pass arguments to a callback that takes no arguments.

We start with this object:

const oracleEndpoints = {
  pancake: () => fetchPancake(),
  lps: () => fetchLP(endpoints.lps), <-- endpoints.lps is an API url
};

And this map:

const oracleToIds = new Map()
oracleToIds.set("lps", ["png-snob-avax", "png-png-avax"])
oracleToIds.set("pancake", ["WBNB", "BREW"])

Now this is the part I don't get:

const promises = oracleToIds.keys().map(key => oracleEndpoints[key](oracleToIds.get(key)));

oracleEndpoints[key] is an anonymous callback (() => fetchPancake() or () => fetchLP(endpoints.lps)) and oracleToIds.get(key) is an array of strings.

What's going on in this expression oracleEndpoints[key](oracleToIds.get(key)) then where an unparametised callback is passed an array?

How is it able to do this?

doctopus
  • 5,349
  • 8
  • 53
  • 105
  • Could you give more detail as to what the code does, and how you know that the argument - `oracleToIds.get(key)` - is being used? From glancing at the code I agree with you, this should do nothing. – Robin Zigmond Mar 21 '21 at 17:08

1 Answers1

0

I'd double check if the array argument is actually being used by anything meaningful. It could easily be a mistake in the code caused by confusion of the writer, given the high level of nesting mashed into a single line of the const promises code that you showed.

This would silently cause an error that would only manifest in application usage -- no exception is thrown in JavaScript if you are passing extra params to a function.

Roy
  • 3,574
  • 2
  • 29
  • 39