Python's Equivalent of JavaScript Promises Last Updated : 04 Dec, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report When we work with JavaScript, we often use Promises to handle asynchronous tasks. A Promise is like a promise in real life - it guarantees something will happen in the future. Python doesn't have Promises. In Python, handling asynchronous tasks is a bit different from JavaScript. We can use asyncio, await, and asyncio.gather() to manage multiple tasks. We can handle errors with try and except, just like .catch() in JavaScript Promises. If we need more control, we can even use Future objects.Using asyncio and await in PythonIn Python, asyncio module helps us deal with asynchronous programming. We use async to define a function as asynchronous and await to pause the function until the task is done. Python import asyncio # Asynchronous function that simulates a task async def task(): result = "Task Completed" return result # Main function to call the task and print the result async def main(): result = await task() # Wait for the task to finish print(result) # Run the main function asyncio.run(main()) OutputTask Completed Table of ContentUsing Callbacks Using concurrent.futures (Advanced Option)Using asyncio.gather() Using Callbacks A callback is a function that gets passed as an argument to another function. It will be called when that function finishes its task. This method is simple and easy to understand. Python # Function that does some task def task(callback): # Simulating some work result = "Task Completed" callback(result) # Callback function that handles the result def handle_result(result): print(result) # Calling the task function with the callback task(handle_result) OutputTask Completed Using concurrent.futures (Advanced Option)The concurrent.futures module allows us to run tasks asynchronously using threads or processes. This is useful when we need to run several tasks at once and handle them in the background. Python import concurrent.futures # Function to simulate a task def task(): return "Task Completed" # Using ThreadPoolExecutor to run the task asynchronously with concurrent.futures.ThreadPoolExecutor() as executor: future = executor.submit(task) # Submit the task result = future.result() # Get the result once the task is done print(result) OutputTask Completed Using asyncio.gather() If we have multiple asynchronous tasks and we want to wait for them to finish at the same time, asyncio.gather() is very useful. It allows us to group tasks and wait for all of them to complete. Python import asyncio # Asynchronous function to simulate a task async def task(name): await asyncio.sleep(0.01) return f"Task {name} Completed" # Main function to run multiple tasks concurrently async def main(): tasks = [task(1), task(2), task(3)] results = await asyncio.gather(*tasks) # Run all tasks concurrently and wait for them for result in results: print(result) # Run the main function asyncio.run(main()) OutputTask 1 Completed Task 2 Completed Task 3 Completed Comment More infoAdvertise with us Next Article Future of Python : Exploring Python 4.0 P pragya22r4 Follow Improve Article Tags : Python python Python vs JavaScript Practice Tags : pythonpython Similar Reads Pass function and arguments from node.js to Python Prerequisites: How to run python scripts in node.js using the child_process module. In this article, we are going to learn how to pass functions and arguments from node.js to Python using child_process. Although Node.js is one of the most widely used web development frameworks, it lacks machine lear 4 min read Detect script exit in Python Python is a scripting language. This means that a Python code is executed line by line with the help of a Python interpreter. When a python interpreter encounters an end-of-file character, it is unable to retrieve any data from the script. This EOF(end-of-file) character is the same as the EOF that 2 min read asyncio in Python Asyncio is a Python library that is used for concurrent programming, including the use of async iterator in Python. It is not multi-threading or multi-processing. Asyncio is used as a foundation for multiple Python asynchronous frameworks that provide high-performance network and web servers, databa 4 min read Future of Python : Exploring Python 4.0 The future of Python 4.0 is a topic of great anticipation and speculation within the tech community. Guido van Rossum, the creator of Python, offers exclusive insights into what might lie ahead for this popular programming language. With Python 3.9 approaching its release, many are curious about t t 8 min read How to communicate JSON data between Python and Node.js ? The following article covers how to communicate JSON data between Python and Node.js. Suppose we are working with the Node.js application, and we want to make use of a specific library that is only available in python or vice versa. We should be able to share the results from one language to another 7 min read Closures And Decorators In Python Closures and decorators are powerful features in Python that allow for more advanced and flexible code patterns. Understanding these concepts can greatly enhance your ability to write clean, efficient, and reusable code.Why Python decorators rather than closures?Python decorators are preferred over 3 min read Like