Introduction
In Python, sometimes we need to delay the execution of a function for a certain amount of time. This can be useful in scenarios where we want to wait for a certain event to occur before executing a function. In this challenge, you will be asked to create a function that delays the execution of another function by a specified amount of time.
Delayed Function Execution
Write a function delay(fn, ms, *args) that takes a function fn, a time in milliseconds ms, and any number of arguments args. The function should delay the execution of fn by ms milliseconds and then invoke it with the provided arguments. The function should return the result of invoking fn.
To delay the execution of fn, use the time.sleep() function. This function takes a number of seconds as an argument, so you will need to convert ms to seconds before passing it to time.sleep().
from time import sleep
def delay(fn, ms, *args):
sleep(ms / 1000)
return fn(*args)
delay(lambda x: print(x), 1000, 'later') ## prints 'later' after one second
Summary
In this challenge, you were asked to create a function that delays the execution of another function by a specified amount of time. You learned how to use the time.sleep() function to delay the execution of a function and how to convert milliseconds to seconds.