JS Code Snippets

jagadeeshshetty

Jagadeesh

Posted on January 29, 2022

JS Code Snippets

Array

1. Find an element in an array using find() method.

let numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

let number = numbers.find(_number => _number === 8);
console.log(number);    // 8

number = numbers.find(_number => _number === 80);
console.log(number);    // undefined
Enter fullscreen mode Exit fullscreen mode

2. tbd

3. tbd

4. tbd

Blocking/NonBlocking or Synchronus/Asynchronus Code

Synchronus or Blocking code

console.log('Before');
console.log('After');

// Result
// Before
// After
Enter fullscreen mode Exit fullscreen mode

Asynchronus or NonBlocking code

console.log('Before');

setTimeout(() => {
    console.log('Reading a user from DB...')
}, 2000);

console.log('After');

// Result
// Before
// After
// Reading a user from DB...
Enter fullscreen mode Exit fullscreen mode

setTimeout() schedules for future run. In this case after 2sec, there will be a console message.

It's not a multithread or parallel run. Still it's a single thread run and due to scheduling, execution is not blocked.

  1. Callback
  2. Promises
  3. Async/Await (syntactic over promise)

1. Callback

  • tbd

2. Promises

  • tbd

3. Async/Await

  • tbd
💖 💪 🙅 🚩
jagadeeshshetty
Jagadeesh

Posted on January 29, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

Where GitOps Meets ClickOps
devops Where GitOps Meets ClickOps

November 29, 2024

How to Use KitOps with MLflow
beginners How to Use KitOps with MLflow

November 29, 2024

Modern C++ for LeetCode 🧑‍💻🚀
leetcode Modern C++ for LeetCode 🧑‍💻🚀

November 29, 2024