Promises

Developer
Size
2,516 Kb
Views
24,288

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 Previews

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);
Promises - Script Codes
Promises - Script Codes
Home Page Home
Developer Steven
Username volv
Uploaded December 07, 2022
Rating 3
Size 2,516 Kb
Views 24,288
Do you need developer help for Promises?

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!

Steven (volv) Script Codes
Create amazing marketing copy with AI!

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!