Bird
0
0

What is wrong with this Promise usage in Angular?

medium📝 Debug Q14 of 15
Angular - RxJS and Observables Fundamentals
What is wrong with this Promise usage in Angular?
const p = new Promise((resolve, reject) => {
  setTimeout(() => resolve('done'), 1000);
});
p.then(result => console.log(result));
p.catch(error => console.error(error));
p.cancel();
APromises do not have a cancel() method, so calling it causes an error.
BThe Promise should use reject() instead of resolve() inside setTimeout.
CThe then() method should come after catch(), not before.
DThe Promise constructor is missing the reject parameter.
Step-by-Step Solution
Solution:
  1. Step 1: Check Promise API for cancellation

    Promises do not support cancellation; there is no cancel() method.
  2. Step 2: Identify error cause

    Calling p.cancel() will cause a runtime error because cancel() is undefined.
  3. Final Answer:

    Promises do not have a cancel() method, so calling it causes an error. -> Option A
  4. Quick Check:

    Promise cancellation is not supported [OK]
Quick Trick: Promises cannot be cancelled; no cancel() method exists [OK]
Common Mistakes:
MISTAKES
  • Thinking Promise supports cancel() like Observable
  • Confusing resolve and reject usage
  • Believing then() must come after catch()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes