Image source from codeburst.io
In this blog i am covering some java script concept. which is quite knowledgeable
JavaScript is a single-threaded, non-blocking language. Asynchronous behavior allows certain operations to be performed without waiting for the result before moving on to the next operation. This is particularly important for tasks that may take some time to complete, such as fetching data from a server, reading a file, or waiting for a user input.
Single-threaded: This means that at any given point in time, only one set of instructions is being executed by the CPU.
non-blocking language: A non-blocking language allows tasks to run independently, so one task doesn't wait for another to finish.
Asynchronous:
Asynchronous means a program can do multiple things at once, like answering messages while still working on a task, instead of waiting for one thing to finish before starting another.
In JavaScript Asynchronous pattern handled in various versions,
ES5 -> Callback
ES6 -> Promise
ES7 -> async & await
Promises: Promises are a way to handle asynchronous operations in JavaScript. When a promise is created, it can be in one of three states: pending, fulfilled, or rejected.
Pending: The initial state of a promise. This is the state when the promise is still executing its asynchronous operation. It hasn't completed yet, nor has it failed.
Fulfilled: The state when the asynchronous operation successfully completes, and the promise has a resulting value.
Rejected: The state when the asynchronous operation fails, and the promise has a reason for the failure
Without async & await
// Creating a promise
const myPromise = new Promise((resolve, reject) => {
//an asynchronous operation
setTimeout(() => {
resolve('Promise resolved');
}, 2000); // delay of 2 seconds
});
// Consuming the promise
myPromise
.then((result) => {
console.log(result);
})
.catch((error) => {
console.error(error);
});
With async & await
// Creating a promise
const myPromise = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Promise resolved');
}, 2000); //delay of 2 seconds
});
};
// Consuming the promise using async/await
const consumePromise = async () => {
try {
const result = await myPromise();
console.log(result);
} catch (error) {
console.error(error);
}
};
// Calling the async function
consumePromise();
The async
keyword in JavaScript is used to define asynchronous functions. An asynchronous function returns a Promise, and within that function, the await
keyword is used to pause the execution until a Promise is resolved.
Mutable Data Types:
In programming, mutability refers to the ability of an object (such as an array or an object) to be changed after it is created.
Objects: Objects in JavaScript are mutable. You can add, modify, or remove properties from an object after it is created.
let person = { name: 'John', age: 25 };
person.age = 26; // Modifying a property
person.job = 'Developer'; // Adding a new property
delete person.age; // Removing a property
//Output
{ name: 'John', job: 'Developer' }
Arrays: Arrays are also mutable. You can modify their elements, add or remove elements, and change their length.
let numbers = [1, 2, 3, 4];
numbers[1] = 5; // Modifying an element
numbers.push(6); // Adding a new element
numbers.pop(); // Removing the last element
// output
[ 1, 5, 3, 4 ]
Immutable Data Types:
Strings: Strings in JavaScript are immutable. Once a string is created, you cannot modify its individual characters.
let text = 'Hello';
// The following line will create a new string,
// not modify the existing one
let newText = text + ' World';
// output
Hello World
Numbers:
Numbers are immutable. Operations that seem to modify a number actually create a new number.
let x = 5;
let y = x + 3; // Creates a new number, does not modify x
console.log(y);
// output
8
Booleans:
Booleans are also immutable.
Thank you for reading my content. Be sure to follow and comment on what you want me to write about next
๐ค