Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Latest commit

 

History

History
81 lines (45 loc) · 4.91 KB

questions.md

File metadata and controls

81 lines (45 loc) · 4.91 KB

Fundamentals

Installation and setup

⚠️ Before preceding, from the project root, cd to the fundamentals directory and run npm it. All tests should fail. ⚠️

Task

Implement solutions for all exercises. Objective: all tests should pass.

Exercises

Construction

  • Complete the implementation of reverseWithCb to reverse a given string using a callback.

  • Complete the implementation of reverseWithPromise to reverse a given string and return the result as a promise. The promise must be created using a Promise constructor.

  • Complete the implementation of createError to yield an instance of Error with a given error message.

  • Complete the implementation of withErrorPromise to return a rejected promise with an instance of Error with a given error message.

  • Complete the implementation of now to immediately return a promise with a given value without using a promise constructor.

  • Harmonic calculates the sum of haronmic numbers up to a given integer n. However, there is an unexpected error. Fix the implementation. Before preceding you'll need to uncomment the implementation and the test

  • Complete the implementation of isUnique to determine if a given string contains unique characters (boolean return type). This function must be implemented as an async function. DO NOT directly use a Promise. Optional: to make the exercise more difficult, only use arrays.

  • Complete the implementation of convert to convert a given third party promise to a native JS promise.

  • Complete the implementation of convert to convert a given third party promise to a native JS promise without using a promise directly.

  • Complete the implementation of toPromise to convert a legacy callback function to return a promise.

  • Complete the implementation of toCallback that converts a given function (that returns a Promise) to a callback based function.

  • Complete the implementation of toAsync that converts a given synchronous function to an async function.

Transformation (chaining)

  • Write an alternative implementation of chain in a new function called chainWithAsync to use async/await. Dont use promises.

  • Complete the implementation of multipleAsyncCalls to create a string containing each letter of the alphabet. Each letter should be created by invoking the generateNextChar async function.

Error handling

  • Complete the implementation of handlePromiseError by:

    • Calling a given service's makeRequest method (service is a parameter to handlePromiseError)
    • Handling any error thrown by .makeRequest() by converting it to an instance of ServiceError using the promise API.
  • Modify the implementation of handlePromiseError to ensure the service's .shutdown method is always called, even if an error is thrown. You must use the Promise API.

  • Write an alternative implementation of handlePromiseError in a new function called handleAsyncError to use async/await. This should include handling the invocation of .shutdown. Dont use promises.

Aggregating results

  • sumOfSquares is an async function that sums the square of each number in a given array. However, it's returning an unexpected result. Fix the implementation. You must use array.reduce_

  • Complete the implementation of aggregateWithPromises by:

    • making 50 requests to service.makeRequest
    • return a promise of all the responses (fulfulment value is an array of responses)
    • Forget about error handling for this exercise
  • Complete the implementation of first by:

    • making 50 requests to service.makeRequest
    • return the first promise to resolve
    • Forget about error handling for this exercise

Advanced - optional exercises

  • Write your own implementation of Promise.all. Refactor aggregateWithPromises to use your implementation.

  • Write your own implementation of Promise.race. Refactor first to use your implementation.

  • Complete the implemention of compress to asynchronously compress a string using the character counts.
    For example:
    compress("aabccccczzzz") // a2b1c5z4

  • Write your own basic implementation of a cancelable promise. You'll need to write tests for this exercise.