I didn’t write yesterday but I have my notes here. We are learning regular expressions now. It is a bit easier than ES6.
I’m about to go to the store here in a bit. We are having taco salad tonight. I want to make pico de gallo with it. I have the Tasting History cookbook. I should make something from the book sometime.
The office is cold. The boys put in the air conditioner for the room. I have my sweater with me in case I need it. I feel like I need it now but I’m going to the store in a few.
Didn’t do much today. I think Tommy is cleaning up the office. Best to stay out of his way while he does that.
Ok, I should be going now…
——————————-
A promise in JavaScript is exactly what it sounds like – you use it to make a promise to do something, usually asynchronously. When the task completes, you either fulfill your promise or fail to do so. Promise is a constructor function, so you need to use the new keyword to create one. It takes a function, as its argument, with two parameters – resolve and reject. These are methods used to determine the outcome of the promise. The syntax looks like this:
const myPromise = new Promise((resolve, reject) => { });
A promise has three states: pending, fulfilled, and rejected. The promise you created in the last challenge is forever stuck in the pending state because you did not add a way to complete the promise. The resolve and reject parameters given to the promise argument are used to do this. resolve is used when you want your promise to succeed, and reject is used when you want it to fail. These are methods that take an argument, as seen below.
const myPromise = new Promise((resolve, reject) => { if(condition here) { resolve("Promise was fulfilled"); } else { reject("Promise was rejected"); } });
The example above uses strings for the argument of these functions, but it can really be anything. Often, it might be an object, that you would use data from, to put on your website or elsewhere.
Promises are most useful when you have a process that takes an unknown amount of time in your code (i.e. something asynchronous), often a server request. When you make a server request it takes some amount of time, and after it completes you usually want to do something with the response from the server. This can be achieved by using the then method. The then method is executed immediately after your promise is fulfilled with resolve. Here’s an example:
myPromise.then(result => { });
result comes from the argument given to the resolve method.
catch is the method used when your promise has been rejected. It is executed immediately after a promise’s reject method is called. Here’s the syntax:
myPromise.catch(error => { });
error is the argument passed in to the reject method.
Regular expressions are used in programming languages to match parts of strings. You create patterns to help you do that matching.
If you want to find the word the in the string The dog chased the cat, you could use the following regular expression: /the/. Notice that quote marks are not required within the regular expression.
JavaScript has multiple ways to use regexes. One way to test a regex is using the .test() method. The .test() method takes the regex, applies it to a string (which is placed inside the parentheses), and returns true or false if your pattern finds something or not.
let testStr = "freeCodeCamp"; let testRegex = /Code/; testRegex.test(testStr);
The test method here returns true.
In the last challenge, you searched for the word Hello using the regular expression /Hello/. That regex searched for a literal match of the string Hello. Here’s another example searching for a literal match of the string Kevin:
let testStr = "Hello, my name is Kevin."; let testRegex = /Kevin/; testRegex.test(testStr);
This test call will return true.
Any other forms of Kevin will not match. For example, the regex /Kevin/ will not match kevin or KEVIN.
let wrongRegex = /kevin/; wrongRegex.test(testStr);
This test call will return false.
A future challenge will show how to match those other forms as well.
Using regexes like /coding/, you can look for the pattern coding in another string.
This is powerful to search single strings, but it’s limited to only one pattern. You can search for multiple patterns using the alternation or OR operator: |.
This operator matches patterns either before or after it. For example, if you wanted to match the strings yes or no, the regex you want is /yes|no/.
You can also search for more than just two patterns. You can do this by adding more patterns with more OR operators separating them, like /yes|no|maybe/.