hard
JavaScript Hard​
What is async await and promises in JavaScript
Generally, in JavaScript, everything is executed sequentially (synchronously), and because of some long-running
function, we have to wait before executing the next line. In such cases, we use the async
keyword in a front of function.
An async function
in JavaScript returns a Promise
and moves on to the next execution task. Promises are indications that these functions will return something. Promises have three states
.
- Pending - it means it have not returned anything and it is in the initial stage.
- Resolved - it means function has returned something.
- Rejected - it means function was not able to return any value due to some
error
.
with help of await
keyword we can make this function to wait until the promise is resolved.
examples:
function longRunningTask() {
return new Promise((resolve, reject) => {
const randomNumber = Math.random() * 10;
setTimeout(() => {
if (randomNumber > 5) {
resolve(
`Task completed successfully with random value: ${randomNumber}`
);
} else {
reject(`Task failed with random value: ${randomNumber}`);
}
}, 5000);
});
}
async function performAsyncTask() {
console.log("Start of async task");
try {
const result = await longRunningTask();
console.log(result);
} catch (error) {
console.error(error);
}
console.log("End of async task");
}
performAsyncTask();
There's a function called longRunningTask()
that act to do a time-consuming
execution by creating a random number. This function uses return Promise
, and the result of this promise can either be a success or failure, depending on a random number. The function takes about 5 seconds to let us know if it worked or not, using setTimeout()
function.
Then, there's another function called performAsyncTask()
. This one uses the await keyword to stop and wait until longRunningTask()
function finishes its execution (to wait for promise to be resolved or rejected).
It logs the Start of async task
, waits for it to finish, and then logs the result or an error
. Finally, it logs End of async task"
.
What is Array and What are the different methods available in Array?
Array: an array is a data structure that allows you to store and organize multiple values in a single variable. It uses a numerical index to access and retrieve individual elements. Arrays are defined using square brackets []
and each element in the array is separated by a comma
. Array index always start from 0
.
const students = ["Harshali", "Chandani", "Rani", "Samiksha"];
console.log(students)[
// output
("Harshali", "Chandani", "Rani", "Samiksh")
];
Methods on Array:
1.lenth: length represents
the number of elements in the array.
2.push(): To add
element from the end of array.
3.pop(): To remove
element from the end of array.
4.unshift(): To insert
element from the start of array.
5.shift(): To delete
elements from the start of array.
6.splice(): This function can perform insertion
and deletion
at once.
7.join(): Convert array to string
default separator is ,
but we can change it.
8.reverse(): Converts an array in reverse
order or decending
order.
9.indexOf( ): indexOf() method is used to find
an array of element.
10.sort(): Converts an array in ascending
order.