Rozwiązania 12.12 #33
kewinzaq1
started this conversation in
Zadania i rozwiązania
Replies: 2 comments
-
export async function conductInterviews(
subjects: string[],
interview: (subject: string) => Promise<string>,
timeConstraint: number
): Promise<string[]> {
const proms = subjects.map(async (promise) => {
const promWithTimeout = withTimeout(interview(promise).then(res => res).catch(e => e), timeConstraint)
try {
return await promWithTimeout
} catch (e) {
return null
}
}
)
return (await Promise.all(proms)).filter(Boolean).map(String)
}
function withTimeout<T>(promise: Promise<T>, timeout: number) {
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => {
reject(new Error('TIMEOUT'))
}, timeout)
})
return Promise.race([promise, timeoutPromise])
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
export async function conductInterviews(
subjects: string[],
interview: (subject: string) => Promise<string>,
timeConstraint: number
): Promise<string[]> {
const completedSubjects: string[] = []
for (const subject of subjects) {
try {
const result = await Promise.race([interview(subject), timeoutPromise(timeConstraint)])
completedSubjects.push(result)
} catch (error: any) {
completedSubjects.push(`Error: ${error.message}`)
}
}
return completedSubjects
}
function timeoutPromise(ms: number): Promise<never> {
return new Promise((_, reject) => {
setTimeout(() => {
reject(new Error('Timeout'))
}, ms)
})
} Jak to jest, że ten kod przechodzi przez trzeci test o niedozwolonym temacie? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Rozwiązania 12.12
Beta Was this translation helpful? Give feedback.
All reactions