Study
-
Consuming PromisesStudy/JavaScript 2020. 5. 15. 18:05
Consuming Promises The initial state of an asynchronous promise is `pending`, but we have a guarantee that it will settle. How do we tell the computer what should happen then? Promise objects come with an aptly named '.then()' method. It allows us to say, “I have a promise, when it settles, then here’s what I want to happen…” In the case of our dishwasher promise, the dishwasher will run then: I..
-
The Node setTimeout() FunctionStudy/JavaScript 2020. 5. 15. 17:46
The Node setTimeout() Function Knowing how to construct a promise is useful, but most of the time, knowing how to consume, or use, promises will be key. Rather than constructing promises, you’ll be handling Promise objects returned to you as the result of an asynchronous operation. These promises will start off pending but settle eventually. Moving forward, we’ll be simulating this by providing ..
-
Constructing a Promise ObjectStudy/JavaScript 2020. 5. 15. 17:13
Constructing a Promise Object Let’s construct a promise! To create a new Promise object, we use the new keyword and the Promise constructor method: const executorFunction = (resolve, reject) => { }; const myFirstPromise = new Promise(executorFunction); The Promise constructor method takes a function parameter called the executor function which runs automatically when the constructor is called. T..
-
What is a Promise?Study/JavaScript 2020. 5. 15. 16:34
What is a Promise? Promises are objects that represent the eventual outcome of an asynchronous operation. A Promise object can be in one of three states: Pending: The initial state— the operation has not completed yet. Fulfilled: The operation has completed successfully and the promise now has a resolved value. For example, a request’s promise might resolve with a JSON object as its value. Rejec..
-
Introduction(promise)Study/JavaScript 2020. 5. 15. 16:23
Introduction In web development, asynchronous programming is notorious for being a challenging topic. An asynchronous operation is one that allows the computer to “move on” to other tasks while waiting for the asynchronous operation to complete. Asynchronous programming means that time-consuming operations don’t have to bring everything else in our programs to a halt. There are countless example..
-
Combining Export StatementsStudy/JavaScript 2020. 5. 14. 20:18
Combining Export Statements We can also use named exports and default exports together. In menu.js: let specialty = ''; function isVegetarian() { }; function isLowSodium() { }; function isGlutenFree() { }; export { specialty as chefsSpecial, isVegetarian as isVeg }; export default isGlutenFree; Here we use the keyword export to export the named exports at the bottom of the file. Meanwhile, we ex..
-
Import asStudy/JavaScript 2020. 5. 14. 20:09
Import as To import named export aliases with the as keyword, we add the aliased variable in our import statement. import { chefsSpecial, isVeg } from './menu'; In orders.js We import chefsSpecial and isVeg from the Menu object. Here, note that we have an option to alias an object that was not previously aliased when exported. For example, the isLowSodium object that we exported could be aliased..
-
Export asStudy/JavaScript 2020. 5. 14. 19:36
Export as Named exports also conveniently offer a way to change the name of variables when we export or import them. We can do this with the as keyword. Let’s see how this works. In our menu.js example let specialty = ''; let isVegetarian = function() { }; let isLowSodium = function() { }; export { specialty as chefsSpecial, isVegetarian as isVeg, isLowSodium }; In the above example, take a look..