MediumNo. 2795LeetCode Premium
Parallel Execution of Promises for Individual Results Retrieval — LeetCode 2795 Python Solution
- Topics
- JavaScript
- 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
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
- Time
- O(n)plus the underlying task time
- Space · auxiliary
- O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 2798 | Number of Employees Who Met the Target | Easy |
| 0057 | Insert Interval | Medium |
| 0192 | Word Frequency | Medium |
| 0193 | Valid Phone Numbers | Easy |
| 0194 | Transpose File | Medium |
| 0195 | Tenth Line | Easy |
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.