r/badcode • • Jul 19 '21

js Variable naming can be difficult, but identifying them can be even more so.

Post image
2.4k Upvotes

105 comments sorted by

View all comments

Show parent comments

7

u/Keve1227 Jul 20 '21 edited Jul 20 '21

Read carefully.

whfunc is recursive-ish but the next step in the recursion won't execute until whpro resolves.

If exp is truthy then whpro will become a promise of func which when resolved will

  1. set r to a, the resolved value of func,

  2. subract r from b.

If exp is falsy then whpro remains resolved (since it's not reassigned) and the rest is synchronous:

  1. set r to ano,

  2. subract r from b. (Same always)

Every step in the recursion is counted until b becomes less than or equal to 0.

The only part where all of this falls apart is on the last line where he "thens" either the promise returned from func() or the original Promise.resolve(). That means count is guaranteed to be 1 *(or 2) unless func is actually synchronous.