loading...

This Week I Learned - Wk 1

snesjhon profile image Jhon Paredes Updated on ・3 min read

This is an ongoing blog about my journey in improving my javascript, algorithm, react skills for future interviews


Aug 30, 2020

Binding

  • A while back I had to answer a question where the answer was binding the function in a setTimeout. This confused me initially because I thought binding a arrow function was possible, but an arrow function is autobinded(?).
  • The way that I’d answer the question is to bind a function which provides a quick way of passing the variable — but only if you do .bind(null, i)
for(var I = 0; I < 5; I++){
  setTimeout(function(j){
    console.log(j);
  }.bind(null, i), 0)
}

Aug 29, 2020

Promises

  • Building a new promise and returned into the new promise.
  • chaining
    • each promise represents the completion of another asynchronous step in the chain.

Aug 28, 2020

Promises

I’ve always known how Promises work, but not WHY they work. This is a blindspot that has come to bite me in the ass. This feels like there’s something missing in my knowledge of learning how this would work. Below is my exploration into why promises happened, based on reading the MDN docs

Using Promises - JavaScript | MDN
16.11: Promises Part 1 - Topics of JavaScript/ES6 - YouTube

Here are the guarantees with promises

  • Callbacks will never be called before the completion of the current run of the JavaScript event loop.
    • run to completion
    • A promise is guaranteed to run to completion through Each message is processed completely before any other message is processed.

Recursion

This fails because we never finish and stack overflow because of the recursion that exists

//fill array with 60000 elements
const list = new Array(60000).join('1.1').split('.');

function removeItemsFromList() {
  var item = list.pop();

  if (item) {
    removeItemsFromList();
  }
};
removeItemsFromList();

As opposed to

const list = new Array(60000).join('1.1').split('.');


// THis works because we let the function finish and then we execute the command
// This is opposed to us actually continously runninging into this recursive loop in which the array is never --fully-- finished and then we end up never really removing everything from the array.
function removeItemsFromList() {
    var item = list.pop();

    if (item) {
      setTimeout(removeItemsFromList, 0);
    }
};

removeItemsFromList();

Because of the setTimeout we wiat until the stack is cleared AND THEN we run the function again to clear and wait for the array to finish. This way we don’t have a partially cleared array and can always have the latest pop`ed array waiting to be inspected and no longer go into the recursion


Aug 27, 2020

Promises

  • In another interview I had there was a question regarding gathering a sudo “fetch” and then sorting it based on the response.
  • I tried to be clever and tries to just load the responses into an array and then add then sort them, but that was the wrong approach, because I’d still run into a race condition where I wouldn’t necessarily have all of the responses back in time to be sorted.
  • I SHOULD’VE put them into a promise. But for whatever reason I just didn’t do it. I also realize that I don’t really have a lot of knowledge on HOW a promise works, so I’ll spend some time learning about promises in the next few days.

Aug 26, 2020

Big O

  • Started studying from a Udemy course on JavaScript algorithms JavaScript (JS) Algorithms and Data Structures Masterclass | Udemy
  • This has enlightened me to rely on simple heuristic on determining what the BigO of a function will be.
  • Although I haven’t experienced this in my normal day-to-day job I feel like this would at least be helpful in getting a quick glance in how it’d affect React functions since ( at least in hooks) they are composed as regular functions.

Discussion

pic
Editor guide