- Open your command line and navigate to your
reposdirectory (if you do not have areposfolder, then you can usemkdir reposto create one) - Use this template repository to start a new project in your repos folder:
git clone <repo_name> - cd
repo_nameto navigate into your new repo directory - Start Visual Studio Code and select 'Open Folder'. Then select
repo_nameto open the folder in the editor (or just typecode .in your terminal inside the repo directory) - Follow the instructions on the README.md file to complete exercises
- Open the app.js file to get started
function watchTurorialCallback(callback, errorCallback) {
let userLeft = false;
let userWatchingLiveStream = true;
if (userLeft) {
errorCallback({
name: "User Left",
message: ":(",
});
} else if (userWatchingLiveStream) {
callback("Thumbs up and Subscribe");
}
}
watchTurorialCallback(
(message) => {
console.log(message);
},
(error) => {
console.log(error.name + " " + error.message);
}
);
- The above function can be replicated as a Promise.
- Declare a variable
watchingand assign it a new promise object let watching = new Promise(); pass in a single callback let wathcing = new Promis ((resolve, reject)=> {
});
2. Inside of the promise constructor, declare a variable named userWatchingLiveStream.
let userWathcingLiveStresm = true;
-
Add a
if/elseconditional that checks ifuserWatchingLiveStreamis true if (userWatchingLiveStream){ resolve ("Thumbs up and Subscribe!"); }else{ reject ("User left"); } -
If
userWatchingLiveStreamis true, call theresolvemethod with "Thumbs up and Subscribe!" if from above -
If false, call the
rejectmethod with "User left." else from above
- Once you have created your new promise:
- Call
watchingand add a promise chain using.thenand.catch - Pass in a function callback to
.thenthat takes in a message andconsole.log's the message - Pass in a function callback to
.catchthat takes in an error andconsole.log's the error watching .then((result) => console.log(result)) .catch((error) => console.log(error));