Sitemap
Ace D Baugh

Ace D Baugh

I am a Fine Art Photographer and Fine Code Programmer. I shoot Sony Raw and am a Full Stack Web Developer

JavaScript Promises

2 min readSep 13, 2022
Press enter or click to view image in full size
Photo by Constantin Wenning on Unsplash

Imagine you were a server at a large restaurant. You are responsible for many tables. How well would you do if you had to seat, take orders, bring drinks, bring food, check on how they were doing, refill drinks, take care of the bill, and clear the table, all before waiting on another table in your sections? I’m assuming not very well (Kubow, 2021). Now imagine you had a website waiting on information to come from a database somewhere else, so you paused loading the rest of the page until you got the whole thing. Or worse, while waiting for some information, the rest of the page crashes because it is dependent upon having that information. How likely would it be for people to use your site? Much like these scenarios, JavaScript promises, promise to solve this dilemma.

Promises are JavaScript objects that have two parts, a section that looks at what it’s waiting for or waiting to happen, and a section to say what will happen when it is finished. Promises have three values of status: pending, fulfilled, and rejected. This means that sections of code will run, like creating listening events on buttons that show data, only after the data is retrieved and the other parts of the code can be processed still while the program waits for the data. Instead of using callback or even nested callbacks, promises use the “.then” keyword to tell the program what will happen once the promise resolves. This is also super helpful, as you can link multiple “.then”s to each other and the program will go sequentially through those(n.d.).

Here is an example of what a snippet of Promise code looks like. It has a timer on it to simulate taking more time as a huge list would from an API (Pavlutin, 2021):

function getList() {

return new Promise((resolve) => {

setTimeout(() => resolve([“apple”, “banana”]), 3000);

});

}

function findFruit(kind) {

Get Ace D Baugh’s stories in your inbox

Join Medium for free to get updates from this writer.

getList().then((list) => {

const found = list.some((fruit) => fruit === kind);

console.log(found);

});

}

findFruit(“apple”); // logs true

References

Javascript promises. (n.d.). Retrieved September 13, 2022, from https://www.w3schools.com/js/js_promise.asp

Pavlutin, D. (2021, July 18). What is a promise in JavaScript? Dmitri Pavlutin Blog. Retrieved September 13, 2022, from https://dmitripavlutin.com/what-is-javascript-promise/

Using promises — javascript: MDN. JavaScript | MDN. (n.d.). Retrieved September 13, 2022, from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises

Kubow, A. (2021). Async JavaScript Part 4: Promises. Retrieved September 13, 2022, from https://www.youtube.com/watch?v=slIJj-zbs_M.

--

--

Ace D Baugh
Ace D Baugh

Written by Ace D Baugh

I am a Fine Art Photographer and Fine Code Programmer. I shoot Sony Raw and am a Full Stack Web Developer