Parallel Execution of Promises for Individual Results Retrieval — LeetCode 2795 Python Solution
MediumLeetCode PremiumJavaScript
- Problem
- #2795
- Reading time
- 4 min
- Source
- leetcode.com
The problem
Given an array functions, return a promise promise. functions is an array of functions that return promises fnPromise.
Example
- Input
- functions = [
- Output
- {"t":100,"values":[{"status":"fulfilled","value":15}]}
- Explanation
- const time = performance.now()
Python solution
Python
import asyncio
import inspect
async def promiseAllSettled(functions):
async def run(fn):
try:
res = fn()
if inspect.isawaitable(res):
res = await res
return res
except Exception as exc:
return exc
results = await asyncio.gather(*(run(fn) for fn in functions))
out = []
for res in results:
if isinstance(res, Exception):
out.append({"status": "rejected", "reason": res})
else:
out.append({"status": "fulfilled", "value": res})
return outComplexity
| Measure | Complexity |
|---|---|
| Time | O(n) plus the underlying task time |
| Space | O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 2795. Parallel Execution of Promises for Individual Results Retrieval?
- LeetCode 2795. Parallel Execution of Promises for Individual Results Retrieval is rated Medium on LeetCode.
- What is the time complexity of LeetCode 2795. Parallel Execution of Promises for Individual Results Retrieval?
- The Python solution on this page runs in O(n) plus the underlying task time.
- What is the space complexity of LeetCode 2795. Parallel Execution of Promises for Individual Results Retrieval?
- The Python solution on this page uses O(n) auxiliary space.
- What topics does LeetCode 2795. Parallel Execution of Promises for Individual Results Retrieval cover?
- LeetCode 2795. Parallel Execution of Promises for Individual Results Retrieval is tagged JavaScript on LeetCode.
- Is LeetCode 2795. Parallel Execution of Promises for Individual Results Retrieval a premium problem?
- Yes. LeetCode 2795. Parallel Execution of Promises for Individual Results Retrieval is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.