Back
Close

JavaScript promises, mastering the asynchronous

Magus
455.2K views

Let's do a harder exercise. In this code, your function receives a parameter data. You must modify the code below based on the following rules:

  • Your function must always return a promise
  • If data is not a number, return a promise rejected instantly and give the data "error" (in a string)
  • If data is an odd number, return a promise resolved 1 second later and give the data "odd" (in a string)
  • If data is an even number, return a promise rejected 2 seconds later and give the data "even" (in a string)
Change the code to return a promise

Hint: When you code a function returning a promise, make sure to always return a promise. Even if you want to handle an error, just return a promise and reject it. You will have more maintenable code like this.

Hint: use isNaN(data) to check if data is not a number

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io