Skip to content
javascriptintermediate

JavaScript Asynchronous Programming

Promise, async/await, fetch

7 questions

By EZ4Code Team

1. What are the three states of a Promise?

pending, fulfilled, rejected
running, done, error
start, middle, end
init, success, fail
Explanation: A Promise has three states: pending (in progress), fulfilled (succeeded), rejected (failed); once changed, the state is irreversible.

2. What does the following code output? Promise.resolve(1) .then(x => x + 1) .then(x => console.log(x))

Promise.resolve(1)
  .then(x => x + 1)
  .then(x => console.log(x))
2
1
undefined
Error
Explanation: resolve(1) passes to the first then getting x=1, returns 2; the second then receives 2 and prints it, outputting 2.

3. What is the return value of an async function?

A Promise
A plain value
A Generator
A callback function
Explanation: An async function always returns a Promise; if it returns a plain value, it is automatically wrapped as a resolved Promise.

4. Where can await only be used?

Inside an async function or in a top-level await context
Anywhere
Only in browsers
Only in Node.js
Explanation: await must be used inside an async function (or top-level await supported by modern environments), otherwise it causes a syntax error.

5. What is the characteristic of Promise.all?

All Promises must succeed to resolve; any failure immediately rejects
Resolves as soon as any one succeeds
Ignores failed Promises
Executes sequentially, not concurrently
Explanation: Promise.all concurrently waits for all Promises; all must be fulfilled to resolve with a result array; any rejection immediately rejects.

6. What does the fetch API return?

A Promise that resolves to a Response object
Directly returns JSON data
A callback function
An XMLHttpRequest object
Explanation: fetch() returns a Promise; the resolved value is a Response object, which needs res.json()/res.text() etc. for further parsing.

7. What is the output order of the following code? console.log(1) setTimeout(() => console.log(2), 0) Promise.resolve().then(() => console.log(3)) console.log(4)

console.log(1)
setTimeout(() => console.log(2), 0)
Promise.resolve().then(() => console.log(3))
console.log(4)
1 4 3 2
1 2 3 4
1 4 2 3
1 3 4 2
Explanation: Synchronous code executes first (1,4); microtasks (Promise.then) take priority over macrotasks (setTimeout), so 3 before 2.

More javascript Quizzes