Basic Promise Structure
by dshilkret
HTML
<div>
In this code, a Promise is created. If success is true, the promise resolves and the .then() method runs. If false, it rejects and .catch() handles the error.
</div>
JavaScript
const myPromise = new Promise((resolve, reject) => {
let success = true;
if (success) {
resolve("Promise has been resolved!");
} else {
reject("Promise has been rejected.");
}
});
myPromise
.then(result => console.log(result))
.catch(error => console.error(error));