Creating a Basic Promise
script.js
index.html
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.
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Awesome!
Completion rate improved to 3.57
Creating a Basic Promise
Pyyhkäise näyttääksesi valikon
script.js
index.html
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.
Kiitos palautteestasi!