How to do asynchronous tasks like this:
I want to have two tasks: One would print "foo" after 1 second, and the other one would print "bar" after 2 seconds. I want them to start at the same time, and run asynchronously.
Here's an example of this in python:
async def main():
task1 = asyncio.create_task(
say_after(1, 'hello'))
task2 = asyncio.create_task(
say_after(2, 'world'))
print(f"started at {time.strftime('%X')}")
# Wait until both tasks are completed (should take
# around 2 seconds.)
await task1
await task2
print(f"finished at {time.strftime('%X')}")
How do I do something like this in JavaScript? Do I use promises? I don't know how they work. I'm "new" to javascript