JS Code Snippets
Jagadeesh
Posted on January 29, 2022
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
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
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...
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.
- Callback
- Promises
- Async/Await (syntactic over promise)
1. Callback
- tbd
2. Promises
- tbd
3. Async/Await
- tbd
💖 💪 🙅 🚩
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.