2

I have a function build that synchronously returns an object, that in turn contains a function run. This function returns a thunk and hence can be called using yield and a library such as co.

Basically the call looks like this:

yield build().run();

Now, the problem is that I want to make sure that the yield refers to run, not to build. How do I do that, without introducing a temporary variable as in the following snippet?

var temp = build();
yield temp.run();

Any ideas?

PS: I'm running this code on Node.js 0.11.x using the ´--harmony´ flag.

Golo Roden
  • 140,679
  • 96
  • 298
  • 425
  • 1
    Little has higher precedence over the member operator and the call operator, so you're safe. Here's a reference for you [MDN Operator Precedence](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence). The `yield` is pretty far down there. Are you having an issue where it isn't working as you expect? – cookie monster May 06 '14 at 19:58
  • Well, I was wondering how to distinguish this from the case where I would want to call `build` using `yield` in case this was the thunkified function. Would that then be `(yield build()).run()`? – Golo Roden May 06 '14 at 20:00
  • 1
    Yes, you'd need the explicit grouping in that case. – cookie monster May 06 '14 at 20:01
  • Great :-)). Thanks for your hint, this perfectly helped me. If you turn your comments into an answer, I will accept it. – Golo Roden May 06 '14 at 20:01

1 Answers1

3

Little has higher precedence over the member operator and the call operator, so you're safe. Here's a reference for you MDN Operator Precedence. The yield is pretty far down there.

If you wanted to group yield to the build() call, you'd need an explicit grouping.

(yield build()).run()
cookie monster
  • 10,671
  • 4
  • 31
  • 45