Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara 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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 2

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Awesome!

Completion rate improved to 3.57

bookCreating a Basic Promise

Scorri per mostrare il menu

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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 2
some-alt