Перейти до основного вмісту
← ⚡ JavaScript & the browser·Модуль C4 · Урок 1
ЗавданняUse Promise.resolve(7).then(...) to chain: first multiply by 2 (return 14), then log the result. Then call Promise.reject(new Error('oops')).catch(e => console.log('caught:', e.message)).

Обіцянки: цінність, якої ще немає

75 XP7 min
Теорія

The 30-second model

A Promise is a placeholder for a value you'll get later. It has three states:

  • pending — the operation is still running.
  • fulfilled — done, here's the value.
  • rejected — done, here's the error.

Once a Promise settles (fulfilled or rejected), the state is permanent. You can attach handlers any time — past or future.

const p = Promise.resolve(42);     // already fulfilled, value 42
const q = Promise.reject(new Error("nope"));  // already rejected

p.then(v => console.log("got", v));         // "got 42"
q.catch(e => console.log("err", e.message)); // "err nope"

.then / .catch / .finally

fetch("/api/user")
  .then(r => r.json())
  .then(user => console.log(user.name))
  .catch(err => console.error("failed:", err))
  .finally(() => hideSpinner());

.then reads "when the previous step resolved." .catch catches any error from anywhere upstream. .finally runs in both cases — perfect for cleanup that should always happen.

Returning from .then

Whatever you return inside .then becomes the value the next .then receives. If you return another Promise, the chain waits for it. That's how the pipeline works.

In 2026, prefer async/await

This lesson teaches you to recognize and reason about Promises. The next lesson (C4-02) shows the prettier syntax that does the same thing — async/await. You'll use that 95% of the time. But every async/await call is still a Promise underneath, so the model in this lesson is what you're really learning.

🔒

Зареєструйся щоб кодувати

Теорія відкрита всім. Інтерактивний редактор, живий перегляд і перевірка відкриваються з 7-денним безкоштовним trial — потрібна картка, скасування у будь-який час.

Реєстрація — безкоштовний trial →

Перші 10 уроків у кожному треку — безкоштовні. Картка не потрібна.

← НазадНаступний урок →

Отримуй одну пораду з Python чи вебу — щодня поштою

Коротко, без води, без спаму. Відписка — в один клік.