Asynchronous chain with timeouts and priorities
JavaScript
Hard
promises
Task «Asynchronous chain with timeouts and priorities»
Implement the function asyncChain(tasks, timeout), which takes an array of tasks (functions returning promises) and a timeout in milliseconds. The function should execute tasks sequentially, but with the ability to skip a task if its execution exceeds the specified timeout. If a task does not complete within the timeout, it should be rejected, and the chain should continue with the next task. The results of successfully completed tasks are collected into an array and returned. If all tasks are skipped, return an empty array. Pay attention to error handling: if a promise completes with an error, it is also considered a task skip.
Input Format
First line: number n — the number of tasks. Then n lines, each containing two integers: delay and result. delay — the delay before resolving the promise (ms), result — the value returned by the promise. Last line: timeout — the maximum execution time for one task (ms).
Output Format
Array of successfully completed task results (numbers), separated by spaces. If the array is empty, output 'none'.
Examples
Example 1
INPUT
2
300 10
200 20
250
OUTPUT
20
Example 2
INPUT
1
500 100
400
OUTPUT
none
Example 3
INPUT
Integer
5
push 10
push 20
peek
pop
pop
OUTPUT
none
main.js