Promises
How do I make an promises?
What is a promises? How do you make a promises? This script and codes were developed by Steven on 07 December 2022, Wednesday.
Promises - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Promises</title>
</head>
<body> <script src="js/index.js"></script>
</body>
</html>
Promises - Script Codes JS Codes
"use strict";
var addToBody = function addToBody(thing) { return document.body.innerHTML += thing + "<br>";
};
var addToConsole = function addToConsole(thing) { return console.log(thing);
};
// No promises (console log)
// Chaining functions that return later.
// Just calling them all at once will cause the total time
// To be 5000ms. And the output would be backwards
// Callback hell. Much nesting.
setTimeout(function () { addToConsole("FIRST after 5000ms"); setTimeout(function () { addToConsole("SECOND after another 4000ms"); setTimeout(function () { addToConsole("THIRD after another 3000ms"); setTimeout(function () { addToConsole("FOURTH after another 2000ms"); setTimeout(function () { addToConsole("FIFTH after another 1000ms"); }, 1000); }, 2000); }, 3000); }, 4000);
}, 5000);
// Promise Examples
var firstThing = function firstThing() { return new Promise(function (resolve, reject) { setTimeout(function () { return resolve(addToBody("FIRST after 5000ms")); }, 5000); });
};
var secondThing = function secondThing() { return new Promise(function (resolve, reject) { setTimeout(function () { return resolve(addToBody("SECOND after another 4000ms")); }, 4000); });
};
var thirdThing = function thirdThing() { return new Promise(function (resolve, reject) { setTimeout(function () { return resolve(addToBody("THIRD after another 3000ms")); }, 3000); });
};
var fourthThing = function fourthThing() { return new Promise(function (resolve, reject) { setTimeout(function () { return resolve(addToBody("FOURTH after another 2000ms")); }, 2000); });
};
var fifthThing = function fifthThing() { return new Promise(function (resolve, reject) { setTimeout(function () { return resolve(addToBody("FIFTH after another 1000ms")); }, 1000); });
};
// Promise example (body output)
// Chained functions in a flatter way. No more nesting
// Intention obvious.
// Boom
firstThing().then(secondThing).then(thirdThing).then(fourthThing).then(fifthThing);
Developer | Steven |
Username | volv |
Uploaded | December 07, 2022 |
Rating | 3 |
Size | 2,516 Kb |
Views | 24,288 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Midas Gift Rolls - Updating | 2,897 Kb |
Camper News | 3,013 Kb |
Recursive Multiplication | 2,119 Kb |
Heavy Snake - Level Editor - Dev | 5,348 Kb |
Closure Example | 2,443 Kb |
Midas - Total Multi | 5,661 Kb |
Twitch Status | 4,190 Kb |
Nested Loops | 2,231 Kb |
Beer Event - Remaining Rewards | 2,537 Kb |
Tic Tac Toe | 4,862 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Awesome | Samarthpd | 2,901 Kb |
Right Click Menu | Anodpixels | 2,252 Kb |
Swiftype Lower Third | Zumwalt | 4,796 Kb |
Expert Help | SinceSidSlid | 4,064 Kb |
Under construction | GhostRider | 1,642 Kb |
LBCA - Mail canvas | Emnbdx | 3,856 Kb |
Coburg Banks SVG Logo | Mjtweaver | 3,875 Kb |
Tree growth tests | Orchard | 3,818 Kb |
Confirm Close Of Foundation Reveal Modal | Winghouchan | 1,818 Kb |
Front in Aracaju Logo Pure CSS | Shankarcabus | 2,581 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!