Creating Custom Promises
script.js
index.html
When you create a custom Promise like checkNumber, you can control how and when it resolves or rejects. The function above returns a new Promise that checks if a given number is greater than 10. If the condition is met, it calls resolve with a success message; otherwise, it calls reject with an error message. To handle the outcome of this Promise, you use the .then method to respond to a successful resolution, and the .catch method to handle a rejection. For instance, calling checkNumber(15) would trigger the .then block, while checkNumber(5) would trigger the .catch block. This pattern helps you manage asynchronous results and errors in a clean and predictable way.
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Can you show me the code for the `checkNumber` function?
Can you explain how Promises work with async/await in this context?
What happens if I pass a non-number value to `checkNumber`?
Awesome!
Completion rate improved to 3.57
Creating Custom Promises
Свайпніть щоб показати меню
script.js
index.html
When you create a custom Promise like checkNumber, you can control how and when it resolves or rejects. The function above returns a new Promise that checks if a given number is greater than 10. If the condition is met, it calls resolve with a success message; otherwise, it calls reject with an error message. To handle the outcome of this Promise, you use the .then method to respond to a successful resolution, and the .catch method to handle a rejection. For instance, calling checkNumber(15) would trigger the .then block, while checkNumber(5) would trigger the .catch block. This pattern helps you manage asynchronous results and errors in a clean and predictable way.
Дякуємо за ваш відгук!