Promises & async/await (concept)
Computer Science · JavaScript
Question 1 of 10
0Inside a promise executor, the resolve function is used to:
Stuck? Read the explanation
Unlocks at a streak of 5 correct answers in a row.
Embed this practice on your site or class page
Paste this HTML into your page. Students practise right there; it is free and needs no account.
Promises & async/await (concept) explained
A promise represents a value that will be available later, such as the result of a network request. The async and await keywords let code that waits for promises read almost like ordinary sequential code.
A promise is pending while its work is running, fulfilled with a value when it succeeds, or rejected with an error when it fails. A settled promise never changes again. An async function always returns a promise. Inside it, await pauses that function until a promise settles and then gives back the value; a rejection throws an error that try and catch can handle.
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function run() {
await wait(100);
return 42;
}
run().then((value) => console.log(value));
Asynchronous work does not block the engine. While one function waits for a timer or a response, other code keeps running, and callbacks run later through the event loop. Awaiting promises one after another runs them in sequence; to run them together, start both and await Promise.all.
Worked example
Suppose two requests take 300 ms and 500 ms. Awaited in sequence, they take about 300 + 500 = 800 ms. Started together with Promise.all, they overlap, so the total is about 500 ms, the time of the slower one.
What you will practise
103 multiple-choice questions (Easy, Medium, Hard). Each answer is followed by an explanation.
Example questions
- A promise represents:
- await pauses until:
- A promise can be:
- async functions always:
- Promise.all:
- The method used to handle a rejected promise is:
- Which keyword pauses an async function until a promise settles?
- Promise.resolve(5) returns:
…and 95 more.