Code Monkey home page Code Monkey logo

node-promises-async-await-lab-abp-11-17's Introduction

Node Promises with Async Await FIXME Lab

Objectives

  1. Implement a Promise within a function with blocking, slow code.
  2. Declare a function as async.
  3. Use await to wait for a promise to resolve.

Instructions

The goal of the lab is to address the two FIXMEs in models/Question.js. The Question has already been provided and implements promises with async and await correctly for the two static class functions, All(), which returns a promise, and PrintAll(), which is an async function that uses await to resolve the promise returned by All(). The tests show this functionality working. Additionally, we've provided a runner you can use to see the functionality in action, printAllQuestions.js, which you can run with node printAllQuestions.js.

// ♥ node printAllQuestions.js 
Printing all questions...
Waiting for Questions to load...
...Questions Loaded


Where in the world is Carmen Sandiego?
What's your favorite TV Show?
What superpower would you want?

As you can see, the sequence of the code is linear and the questions are loaded and only then does the printing continue.

Your job is to make Question.Print(id) work, a similar static class function for printing a specific question by ID (which is the index of the question in the Question._All array - 1, i.e const question = Question._All[id-1]).

We've given you a runner for this behavior as well, printFirstQuestion.js. Currently that is broken because Print(id) and Find(id) fail to implement promises and await and async.

// ♥ node printFirstQuestion.js 
Printing first question...
Waiting for Question 1 to load...


/Users/avi/Development/code/accelerated-bootcamp-prep/week-2/node-promises-async-await-lab/models/Question.js:47
    console.log(question.content)
                         ^

TypeError: Cannot read property 'content' of undefined

That error is happening because the Print(id) function is trying to print the question's content before it has actually been returned.

FIXME: Question.Find(id)

Within the Question class you'll find a non-functioning Find(id) static class method. The first thing to do is make this function return a promise that wraps the setTimeout. The promise you implement should resolve with the question.

FIXME: Question.Print(id)

Once Question.Find(id) returns a promise that will resolve by returning the found question, you have to make Question.Print(id) an async function. Once that function is declared as async, you will be able to use await in front of Question.Find(id) to force the code to wait for the promise to resolve and return the question before trying to print.

Use the template that is implemented in All() and PrintAll() for help and syntax help.

Resources

node-promises-async-await-lab-abp-11-17's People

Contributors

aviflombaum avatar gj avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.