Open Side Menu Go to the Top
Register
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

09-13-2018 , 05:39 AM
Quote:
Originally Posted by goofyballer
Function binding? I forget the exact syntax, but something like

Code:
for (i=0;i<5;i++) {
  window.setTimeout((function(j) {
    console.log(j)
  }).bind(null, i), i * 100)
}
(null first because iirc the first arg to bind sets the value of `this`)
(that may well have the same problem though and idk what the hot codepen sites are to actually try it)
Yes, this is a "fancier" way I had in mind, but I rarely ever use bind anymore because arrow functions so it doesn't come as easy, nice!

Quote:
Originally Posted by suzzer99
Heh, apparently this works too.

Code:
/** Console.log out 0-4 inside the timeout */
for(i=0;i<5;i++) {
  window.setTimeout(function(index) {
    console.log(index)
  }, i * 100, i)
}
https://stackoverflow.com/questions/...meout-callback

[third argument to setTimeout]

Crazy - I never knew that. Works in node too.
Index, nice, going to remember that!

[3rd argument] - I used this once for something very specific and I forget what, but at the time it seemed like magic!
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-13-2018 , 12:24 PM
Quote:
Originally Posted by Larry Legend
If you move your promise1.then outside of the definition of the promise, then you get 0-4:

Code:
for (i=0;i<5;i++) {
    var promise1 = new Promise(function(resolve, reject) {
      setTimeout(resolve(i), i*100);
    });     
    promise1.then((successMessage) => {
      console.log(successMessage);
    });
}
Thanks. That works. Can someone explain why the first one didn't work? "promise1.then" can't happen after the loop is finished for the last time if it's inside the promise callback function? How is this different than the next to the last time? The loop is actually done before that time as well, no?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-13-2018 , 01:03 PM
I've never seen a promise.then called inside the definition of the promise.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-13-2018 , 01:17 PM
Quote:
Originally Posted by suzzer99
I've never seen a promise.then called inside the definition of the promise.
I never had an idea what a promise was until I heard it on Javascript Jabber yesterday. I've been on semi-hiatus since about 2005. Just trying to get a handle on it and wonder why it specifically worked all but the last time.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-13-2018 , 01:50 PM
Ok lets walk through it. I suspect what's happening is the 0 is actually being spit out on the second pass, so it never gets to the last pass. Which we can verify by reducing the loop size to 2:

Code:
for (i=0;i<2;i++) {
  console.log('i='+i);  
  var promise1 = new Promise(function(resolve, reject) {
        setTimeout(resolve(i), i*100);
        promise1.then((successMessage) => {
          console.log(successMessage);
        });     
     });
  }
Output: 0. So that confirms the first pass is doing nothing. Why? Well lets take the loop out of the equation and think about it:

Code:
var promise1 = new Promise(function(resolve, reject) {
  setTimeout(resolve(i), i*100);
  promise1.then((successMessage) => {
     console.log(successMessage);
  });     
});
Output: (nothing, as expected). Is it something weird caused by the setTimeout? Doesn't feel like it, but let's take that out of the equation:

Code:
var promise1 = new Promise(function(resolve, reject) {
  resolve(i);
  promise1.then((successMessage) => {
     console.log(successMessage);
  });     
});
Output: (still nothing). What's left? Maybe it's something weird in that we're referencing var promise1 inside its own definition. So let's test for with a console.log:

Code:
var promise1 = new Promise(function(resolve, reject) {
  resolve(i);
  console.log(promise1);
  promise1.then((successMessage) => {
     console.log(successMessage);
  });     
});
Output: undefined. So yeah that confirms it. What happens is it's not defined yet the first time through on the loop. On the second time through it's actually executing the promise1 you defined on the first time through - hence output: 0. It makes sense that a variable wouldn't be defined yet, until its definition block is done being interpolated by the compiler.

So why doesn't it throw an error on the first pass? Well all I can think of is maybe the Promise constructor swallows exceptions. So let's test for that by wrapping the .then definition in a try/catch block:

Code:
var promise1 = new Promise(function(resolve, reject) {
  setTimeout(resolve(0), 100);
  console.log(promise1);
  try {
    promise1.then((successMessage) => {
      console.log(successMessage);
    });     
  }
  catch(e) {console.log(e);}
});
Output:
Quote:
TypeError: Cannot read property 'then' of undefined
at VM356 pen.js:7
at new Promise (<anonymous>)
at VM356 pen.js:3
Yep - the Promise constructor is swallowing the exception. Bad Promise constructor. I guess the JS overlords are do as I say, not as I do.

Let's wrap the whole thing in a try/catch block, just on the off chance it's some weird codepen sandbox behavior that's causing the error not to show up:

Code:
    try {
  var promise1 = new Promise(function(resolve, reject) {
    setTimeout(resolve(i), i* 100);
      promise1.then((successMessage) => {
        console.log(successMessage);
      });     
  });
}
    catch(e) {console.log(e);}
Output: nothing. So yeah, not a weird codepen thing.

So just for fun let's add the loop back in with the try/catch block:

Code:
for (i=0;i<5;i++) {
  var promise1 = new Promise(function(resolve, reject) {
    setTimeout(resolve(i), i* 100);
    try {
      promise1.then((successMessage) => {
        console.log(successMessage);
      });     
    }
    catch(e) {console.log(e);}
  });
}
Output:
Quote:
TypeError: Cannot read property 'then' of undefined
at VM356 pen.js:5
at new Promise (<anonymous>)
at VM356 pen.js:2

0

1

2

3
You can play around with it here: https://codepen.io/anon/pen/oPdaPm?editors=0012

Really digging codepen over jsFiddle. The only thing you need to make sure to do is change Settings: Behavior to not enable Auto-Updating Preview.

Last edited by suzzer99; 09-13-2018 at 02:13 PM.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-13-2018 , 02:48 PM
Quote:
Originally Posted by Larry Legend
This is very similar to an interview question I had for my current job. Traverse an object of unknown complexity and length and then the interviewer added a few changing requirements. Ternary because why not?

Code:
var traverseElements = function(element, callback) {
  element.children ? traverseElements(element.children, callback) : callback(element)
}
Wouldn't this only execute `callback` on child-less nodes?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-13-2018 , 02:55 PM
Quote:
Originally Posted by Larry Legend
Yes, this is a "fancier" way I had in mind, but I rarely ever use bind anymore because arrow functions so it doesn't come as easy, nice!
What would arrow function binding look like?

Code:
for (i=0;i<5;i++) {
  window.setTimeout(() => console.log(i), i * 100)
}
Does arrow function capturing have different rules that would make the above do the right thing? Or would it be like suzzer's function-in-yo-function thing in arrow syntax?

I recall that arrow functions bind `this` "correctly", as opposed to older syntax requiring stupid "var thiz = this" stuff, but not sure if anything else differs.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-13-2018 , 03:16 PM
You need a factory function to get the scoping to work right if you're using a variable that is not block scoped, even with arrow functions, e.g.

Code:
for (i = 0; i < 5; i++) {
  const fn = (n) => () => console.log(n);
  window.setTimeout(fn(i), i * 100);
}
But of course if you're allowed ES6 then you should just let i = 0...
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-13-2018 , 04:49 PM
Suzzer,

Thanks. In the podcast they talked about the issues with error catching and promises. One of the guests was there talking about Async/Await and I think that makes error handling easier.

I'm going to verify I get this on the code that worked:

for (i=0;i<5;i++) {
var promise1 = new Promise(function(resolve, reject) {
setTimeout(resolve(i), i*100);
}); HERE
promise1.then((successMessage) => {
console.log(successMessage);
});
}

Now, my vocabulary probably sucks and any corrections are more than welcome, it's not until HERE that the "promise1" object is defined and only after that point can its "then" method be called.

?

And, wow, JSFiddle looks cool. Man people have spent a lot of time developing tools in the last 13 years.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-13-2018 , 05:20 PM
Quote:
Originally Posted by goofyballer
Wouldn't this only execute `callback` on child-less nodes?
Yup I'm an idiot
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-13-2018 , 05:23 PM
Micro, yes, because as you can see you are creating the variable promise1 and assigning it to a new instance of Promise. That assignment is not complete until the block closes - }); So you can't put an instance of the variable inside the definition.

It's like telling someone "Make a ball and call it George: #1 get some plastic. #2 Heat plastic. Now bounce George! #3 Pour into mold. #4 Let cool."

George isn't even created yet so you can't bounce him.

Last edited by suzzer99; 09-13-2018 at 05:29 PM.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-14-2018 , 09:15 AM
The more I read this thread, the less I like javascript.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-14-2018 , 10:14 AM
Quote:
Originally Posted by Wolfram
The more I read this thread, the less I like javascript.


(don't be confused by the minute of ruby at the beginning :P)
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-14-2018 , 10:39 AM
Count me in on the JavaScript hate train. I don’t know how you guys do this day in and day out.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-14-2018 , 10:52 AM
Quote:
Originally Posted by well named




(don't be confused by the minute of ruby at the beginning :P)


That video’s amazing, i want more
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-14-2018 , 11:26 AM
Quote:
Originally Posted by jmakin
Count me in on the JavaScript hate train. I don’t know how you guys do this day in and day out.
All of our JS discussions here are way deeper into the pointlessly arcane than my day to day use of the language. It probably creates a slightly skewed perception. Some of that, I think, is just that JS devs consume libraries and do other things to sort of polish off the rough edges and avoid the weird parts. Some of it is the tendency of interview questions to explore the arcane bits. Also everything seems much worse if you have to support like IE9 or some ****.

Although, I think understanding variable scoping for asynchronous execution is probably pretty important, even if the specific example is contrived.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-14-2018 , 12:21 PM
Quote:
Originally Posted by jmakin
Count me in on the JavaScript hate train. I don’t know how you guys do this day in and day out.
we dont do this day in day out or really ever. at least I dont, and I use Javascript all day pretty much.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-14-2018 , 01:07 PM
Quote:
Originally Posted by goofyballer
What would arrow function binding look like?

Code:
for (i=0;i<5;i++) {
  window.setTimeout(() => console.log(i), i * 100)
}
Does arrow function capturing have different rules that would make the above do the right thing? Or would it be like suzzer's function-in-yo-function thing in arrow syntax?

I recall that arrow functions bind `this` "correctly", as opposed to older syntax requiring stupid "var thiz = this" stuff, but not sure if anything else differs.
Take this with a grain of salt ofc, but this with arrow functions basically isn't a thing. So using this in an arrow function is just a lexically bound meaning of this.

In react that's important for stuff like click events, where you are calling a method that's on the class, but calling it from the dom where this is mostly window. So you need to bind that event's this reference in the class's constructor. With arrow functions you get that binding with less characters.

Interestingly, on a Dan Abramov podcast they were discussing performance impact of binding in constructor and using an arrow function and another guest said the arrow function was more performant. Abramov quickly corrected that, saying he "would not be confident that is correct" and it started a quick discussion about which strategy was actually more performant, which they all said they had no direct idea. I tried to research it quickly and wasn't able to find what I was looking for (an autistically detailed explanation of the exact inplementations).

I was reading code a 30+ year dev wrote at my last company for a last minute project and she was both using arrow functions AND binding in the constructor. THAT I'm pretty sure is a performance penalty where you end up with at least an extra copy of every method you dont need (maybe two?) but I wasnt even sure that it didnt somehow get parsed out more efficiently by the interpreter.

I also found a bunch of binding inside JSX but the performance issues with that are more obvious (every re-render causes you to store an extra copy of that function).
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-14-2018 , 01:09 PM
Yea I guess I could probably conjure up some obscure and weird C code examples. I did a project for a website from scratch earlier this year and I heavily used vanilla JS. I really, really didn’t like it or the tools at all.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-14-2018 , 01:59 PM
Quote:
Hey suzzer,

Triplebyte is opening up to engineers and companies hiring them in Seattle and Los Angeles on September 24th! If you introduce us to hiring managers or technical recruiters based in either city, we'll give you a $1,500 bonus if they make a hire through us. We're also offering the companies a special discounted hiring fee for their first hire.

Here's some recommended text you could use, it includes a referral link unique to you:

---
Hey X,

I wanted to let you know that Triplebyte, a marketplace for hiring engineers that's used by Apple and Dropbox, is expanding to work with companies in [Seattle/LA]. They're officially going live on the 24th September.

Triplebyte does a rigorous technical pre-screen of all engineers and see's an average onsite to offer rate of 40%, twice the industry standard. There's no upfront fees, you only pay if you make a hire and they're offering a special discounted fee of $15,000 on your first hire if it's made before the end of the year.

You can sign up to use them here https://triplebyte.com/ivc/cV1Z9xa/me. They also have some case studies with some of their companies on their website that explain more about how they work too: https://triplebyte.com/company-case-studies

Thanks,
suzzer
---

Triplebyte
FYI - if anyone gets a job I'll split the referral bonus with you.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-14-2018 , 02:51 PM
Hah, must feel good to be able to spam recruiters for a change!

Last edited by jjshabado; 09-14-2018 at 02:52 PM. Reason: Don't forget to text Jon!
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-14-2018 , 03:15 PM
Quick yes/no!
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-14-2018 , 05:11 PM
I might have to create a new LinkedIn account just to do that. Anyone have experience in creating fake LinkedIn profiles?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-14-2018 , 05:34 PM
I was told by someone who uses several that "it's much harder now than it used to be".

She saw it as essentially "growth hacking" and had accounts of hot chicks she used for getting the first response and then quickly handing it off. Random people would be using the account and then handing the leads off (often to themselves).

I thought it was meh fine, but kinda shady and not the type of thing I would plan to do longterm at all.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-14-2018 , 07:37 PM
FYI - I posted that for the $1500 referral thing, not the $15000.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote

      
m