Two Plus Two Publishing LLC
Two Plus Two Publishing LLC
 

Go Back   Two Plus Two Poker Forums > >

Notices

Programming Discussions about computer programming

Reply
 
Thread Tools Display Modes
Old 09-13-2018, 05:30 AM   #35201
Larry Legend
Celtic Pride
 
Larry Legend's Avatar
 
Join Date: Jul 2009
Location: Kyrie's earth
Posts: 42,452
Spade Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by microbet View Post
I was listening to a podcast and they were talking about promises. I did this on my phone. It did

0
1
2
3

I think the 4 should come but the thing I was using cut off the output at 4 lines.

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);
        });     
     });
  }
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);
    });
}
Larry Legend is offline   Reply With Quote
Old 09-13-2018, 05:39 AM   #35202
Larry Legend
Celtic Pride
 
Larry Legend's Avatar
 
Join Date: Jul 2009
Location: Kyrie's earth
Posts: 42,452
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by goofyballer View Post
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 View Post
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!
Larry Legend is offline   Reply With Quote
Old 09-13-2018, 12:24 PM   #35203
microbet
Solar Powered
 
microbet's Avatar
 
Join Date: Jan 2005
Location: SoCal
Posts: 49,860
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by Larry Legend View Post
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?
microbet is offline   Reply With Quote
Old 09-13-2018, 01:03 PM   #35204
suzzer99
Carpal \'Tunnel
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 87,076
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

I've never seen a promise.then called inside the definition of the promise.
suzzer99 is offline   Reply With Quote
Old 09-13-2018, 01:17 PM   #35205
microbet
Solar Powered
 
microbet's Avatar
 
Join Date: Jan 2005
Location: SoCal
Posts: 49,860
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by suzzer99 View Post
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.
microbet is offline   Reply With Quote
Old 09-13-2018, 01:50 PM   #35206
suzzer99
Carpal \'Tunnel
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 87,076
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

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.
suzzer99 is offline   Reply With Quote
Old 09-13-2018, 02:48 PM   #35207
goofyballer
 
goofyballer's Avatar
 
Join Date: Jun 2005
Posts: 63,882
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by Larry Legend View Post
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?
goofyballer is offline   Reply With Quote
Old 09-13-2018, 02:55 PM   #35208
goofyballer
 
goofyballer's Avatar
 
Join Date: Jun 2005
Posts: 63,882
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by Larry Legend View Post
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.
goofyballer is offline   Reply With Quote
Old 09-13-2018, 03:16 PM   #35209
well named
poorly undertitled
 
well named's Avatar
 
Join Date: Jun 2007
Location: esse est coesse
Posts: 74,372
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

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...
well named is offline   Reply With Quote
Old 09-13-2018, 04:49 PM   #35210
microbet
Solar Powered
 
microbet's Avatar
 
Join Date: Jan 2005
Location: SoCal
Posts: 49,860
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

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.
microbet is offline   Reply With Quote
Old 09-13-2018, 05:20 PM   #35211
Larry Legend
Celtic Pride
 
Larry Legend's Avatar
 
Join Date: Jul 2009
Location: Kyrie's earth
Posts: 42,452
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by goofyballer View Post
Wouldn't this only execute `callback` on child-less nodes?
Yup I'm an idiot
Larry Legend is offline   Reply With Quote
Old 09-13-2018, 05:23 PM   #35212
suzzer99
Carpal \'Tunnel
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 87,076
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

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.
suzzer99 is offline   Reply With Quote
Old 09-14-2018, 09:15 AM   #35213
Wolfram
Carpal \'Tunnel
 
Wolfram's Avatar
 
Join Date: Jan 2006
Location: You can be my wingman any time
Posts: 14,700
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

The more I read this thread, the less I like javascript.
Wolfram is offline   Reply With Quote
Old 09-14-2018, 10:14 AM   #35214
well named
poorly undertitled
 
well named's Avatar
 
Join Date: Jun 2007
Location: esse est coesse
Posts: 74,372
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by Wolfram View Post
The more I read this thread, the less I like javascript.


(don't be confused by the minute of ruby at the beginning :P)
well named is offline   Reply With Quote
Old 09-14-2018, 10:39 AM   #35215
jmakin
 
jmakin's Avatar
 
Join Date: Jan 2008
Location: Streaming
Posts: 28,468
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Count me in on the JavaScript hate train. I donít know how you guys do this day in and day out.
jmakin is offline   Reply With Quote
Old 09-14-2018, 10:52 AM   #35216
jmakin
 
jmakin's Avatar
 
Join Date: Jan 2008
Location: Streaming
Posts: 28,468
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by well named View Post




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


That videoís amazing, i want more
jmakin is offline   Reply With Quote
Old 09-14-2018, 11:26 AM   #35217
well named
poorly undertitled
 
well named's Avatar
 
Join Date: Jun 2007
Location: esse est coesse
Posts: 74,372
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by jmakin View Post
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.
well named is offline   Reply With Quote
Old 09-14-2018, 12:21 PM   #35218
Victor
Carpal \'Tunnel
 
Victor's Avatar
 
Join Date: Jul 2003
Posts: 59,976
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by jmakin View Post
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.
Victor is offline   Reply With Quote
Old 09-14-2018, 01:07 PM   #35219
Larry Legend
Celtic Pride
 
Larry Legend's Avatar
 
Join Date: Jul 2009
Location: Kyrie's earth
Posts: 42,452
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by goofyballer View Post
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).
Larry Legend is offline   Reply With Quote
Old 09-14-2018, 01:09 PM   #35220
jmakin
 
jmakin's Avatar
 
Join Date: Jan 2008
Location: Streaming
Posts: 28,468
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

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.
jmakin is offline   Reply With Quote
Old 09-14-2018, 01:59 PM   #35221
suzzer99
Carpal \'Tunnel
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 87,076
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

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.
suzzer99 is offline   Reply With Quote
Old 09-14-2018, 02:51 PM   #35222
jjshabado
Carpal Tunnel
 
jjshabado's Avatar
 
Join Date: Jul 2006
Posts: 22,284
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

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!
jjshabado is offline   Reply With Quote
Old 09-14-2018, 03:15 PM   #35223
suzzer99
Carpal \'Tunnel
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 87,076
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quick yes/no!
suzzer99 is offline   Reply With Quote
Old 09-14-2018, 05:11 PM   #35224
Craggoo
culled
 
Craggoo's Avatar
 
Join Date: Sep 2006
Location: R.I.P. ItzPenzoo 12-09-11
Posts: 12,370
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

I might have to create a new LinkedIn account just to do that. Anyone have experience in creating fake LinkedIn profiles?
Craggoo is offline   Reply With Quote
Old 09-14-2018, 05:34 PM   #35225
Larry Legend
Celtic Pride
 
Larry Legend's Avatar
 
Join Date: Jul 2009
Location: Kyrie's earth
Posts: 42,452
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

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.
Larry Legend is offline   Reply With Quote

Reply
      

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Forum Jump


All times are GMT -4. The time now is 06:47 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
Copyright © 2008-2017, Two Plus Two Interactive
 
 
Poker Players - Streaming Live Online