// Function that returns a Promise
function customPromise() {
return new Promise((resolve, reject) => {
// Simulate asynchronous operation with a setTimeout
setTimeout(() => {
// Resolve the promise with a message after 1000 milliseconds
resolve('Promise resolved!');
}, 1000);
});
}
// Call the function and handle the resolved promise
customPromise().then(result => {
console.log(result); // Output: Promise resolved!
});
code
JavaScript Promise Example
This JavaScript code demonstrates the use of Promises. The customPromise function returns a Promise that resolves after a simulated asynchronous operation using setTimeout. The then method is used to handle the resolved promise and log the result to the console. The comments provide explanations for each part of the code.
Asynchronous
Promise
Resolve
Timeout