Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Creating a Basic Promise | Mastering Promises
Asynchronous JavaScript Explained

bookCreating a Basic Promise

script.js

script.js

index.html

index.html

copy

When you create a new Promise in JavaScript, it starts in the pending state. In the example above, the myPromise object is created using the Promise constructor, which takes a function with two arguments: resolve and reject. Inside this function, you can perform asynchronous operations. Here, a setTimeout is used to simulate a delay. After one second, the resolve function is called with the value "Promise fulfilled!". This action moves the promise from the pending state to the fulfilled state. Once the promise is fulfilled, any .then() handlers attached to the promise will execute, receiving the resolved value. This pattern allows you to write asynchronous code that is easier to read and manage than deeply nested callbacks.

question mark

What does the resolve function do in a Promise?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 2

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Awesome!

Completion rate improved to 3.57

bookCreating a Basic Promise

Pyyhkäise näyttääksesi valikon

script.js

script.js

index.html

index.html

copy

When you create a new Promise in JavaScript, it starts in the pending state. In the example above, the myPromise object is created using the Promise constructor, which takes a function with two arguments: resolve and reject. Inside this function, you can perform asynchronous operations. Here, a setTimeout is used to simulate a delay. After one second, the resolve function is called with the value "Promise fulfilled!". This action moves the promise from the pending state to the fulfilled state. Once the promise is fulfilled, any .then() handlers attached to the promise will execute, receiving the resolved value. This pattern allows you to write asynchronous code that is easier to read and manage than deeply nested callbacks.

question mark

What does the resolve function do in a Promise?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 2
some-alt