What does 'asynchronous' mean in JavaScript?
A
Code that runs immediately B
Code that runs line by line C
Code that runs later, without blocking the main thread D
Code that never executes
Analysis & Theory
Asynchronous code executes later, allowing the main thread to continue running.
Which JavaScript function is used to delay code execution?
A
delay() B
setInterval() C
pause() D
setTimeout()
Analysis & Theory
`setTimeout()` delays code execution by a specified time (in milliseconds).
What will this output?
```js
console.log('1');
setTimeout(() => console.log('2'), 0);
console.log('3');
```
A
`1 2 3` B
`2 1 3` C
`1 3 2` D
`3 1 2`
Analysis & Theory
`setTimeout` is asynchronous, so `'2'` is logged after `'3'`.
What is a Promise in JavaScript?
A
A type of loop B
An if-else condition C
An object representing the eventual result of an asynchronous operation D
A function that always fails
Analysis & Theory
A Promise is used to handle asynchronous operations and represents a value that may be available now or later.
What are the possible states of a Promise?
A
Start, Wait, End B
Ready, Running, Done C
Pending, Fulfilled, Rejected D
Wait, Process, Error
Analysis & Theory
A Promise can be in one of three states: Pending, Fulfilled, or Rejected.
What is the purpose of `.then()` in Promises?
A
To handle errors B
To execute code after a delay C
To define what happens when the promise is fulfilled D
To cancel a promise
Analysis & Theory
The `.then()` method is used to run code when a Promise is fulfilled.
What is `async` used for in JavaScript?
A
To declare asynchronous functions B
To create delays C
To loop asynchronously D
To cancel a function
Analysis & Theory
`async` is used before a function to enable the use of `await` inside it.
What is the role of `await` in an `async` function?
A
It skips code B
It stops code forever C
It pauses execution until the Promise resolves D
It handles errors
Analysis & Theory
`await` pauses the function until the Promise is resolved.
Which of the following is **true** about async/await?
A
It blocks the entire JavaScript thread B
It makes async code easier to read like synchronous code C
It replaces all callback functions D
It doesn’t support error handling
Analysis & Theory
`async/await` makes it easier to read and write asynchronous code.
How can you handle errors in async/await code?
A
Using `.catch()` only B
Using `try...catch` block C
Using `throw()` outside a function D
You can’t handle errors
Analysis & Theory
`try...catch` is used to handle errors in `async` functions.