function startPromise() {
var p = new Promise(function(resolve, reject) {
window.setTimeout(function() {
var optionsSelect = document.getElementById("promiseOptionSelect");
if (optionsSelect.options[optionsSelect.selectedIndex].value === "yes") {
resolve("User chose to resolve this promise");
} else {
reject("User chose not to resolve this promise");
}
}, Math.random() * 2000 + 1000); //timeout of 1-2 seconds
});
// The canonical use for promises is calling out to some HTTP service to get data.
// We cannot process the data until we get it, so we delay that processing inside the
// then() function. If the data is unavailable for some reason, we will not be able
// to proccess the data, so we implement the catch() to handle this result.
p.then(function(data) { //then() is executed when resolve() is called inside the promise
document.getElementById("promiseResult").innerHTML = "promise was fulfilled: " + data;
}).catch(function(reason) { //catch() is executed when reject() is called inside the promise
document.getElementById("promiseResult").innerHTML = "promise was rejected: " + reason;
});
}
let updateValue = function(value) {
return new Promise((resolve, reject) => {
window.setTimeout(function() {
document.getElementById("chainingResult").innerHTML += value.toString() + " | ";
resolve(value + 1);
}, 2000);
});
};
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.