You are developing a task scheduling system where tasks must be executed asynchronously. Each task will take a specified amount of time to complete, and the system needs to log appropriate messages based on whether the task succeeds or fails.
Write a function executeTask
that accepts two parameters.
taskName
: A string representing the name of the task.
taskDuration
: A number representing the duration of the task in milliseconds.
The executeTask
function should return a promise. The promise should resolve with the message <taskName> completed successfully in <taskDuration> ms. after the specified duration and reject with the message <taskName> failed: Invalid duration. if the provided taskDuration is negative.
Next, create another function runScheduler
that also takes two parameters.
taskName
: A string representing the name of the task.
taskDuration
: A number representing the duration of the task in milliseconds.
In the runScheduler
function, call the executeTask function with the provided taskName
and taskDuration
. Use async/await to handle the promise. If the task completes successfully, log the success message and if it fails, catch the error and log it.
Regardless of the task's success or failure, always log the completionMessage
to the console after the task is complete.