JSFiddle - React, Tailwind, and code Playground
by tobyworth
HTML
<ul id="app"></ul>
JavaScript
let app = document.getElementById("app");
app.insertAdjacentHTML('afterend', "<li>first</li>");
let j = JSON.parse('{"resolved": true}');
let prom = function(val, time = 1000){
let p = new Promise(function(resolve, reject){
setTimeout(() => {
if (val){
resolve('{"resolved": true}');
}
else{
reject('{"rejected": true}');
}
}, time);
});
return p;
};
var callbacks = {
success: function(data){
console.log(1, "success", JSON.parse(data));
app.insertAdjacentHTML('afterend', "<li>" + data + "</li>");
},
error: function(data){
console.log(2, "error", JSON.parse(data));
app.insertAdjacentHTML('afterend', "<li>" + data + "</li>");
}
}
prom(true, 500).then(
callbacks.success,
callbacks.error
);
prom(false, 2000)
.then(callbacks.success)
.catch(callbacks.error);