All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2601_2700.s2650_design_cancellable_function.solution.ts Maven / Gradle / Ivy

The newest version!
// #Hard #2023_09_06_Time_58_ms_(93.15%)_Space_43.5_MB_(17.81%)

function cancellable(generator: Generator, T, unknown>): [() => void, Promise] {
    let isCancelled = false

    function cancel() {
        isCancelled = true
    }

    const promise = new Promise(async (resolve, reject) => { //NOSONAR
        try {
            let lastYielded = generator.next()
            while (!lastYielded.done) {
                try {
                    const res = await lastYielded.value
                    lastYielded = isCancelled ? generator.throw('Cancelled') : generator.next(res)
                } catch (err) {
                    lastYielded = generator.throw(err)
                }
            }
            resolve(lastYielded.value)
        } catch (err) {
            reject(err)
        }
    })

    return [cancel, promise]
}

/*
 * function* tasks() {
 *   const val = yield new Promise(resolve => resolve(2 + 2));
 *   yield new Promise(resolve => setTimeout(resolve, 100));
 *   return val + 1;
 * }
 * const [cancel, promise] = cancellable(tasks());
 * setTimeout(cancel, 50);
 * promise.catch(console.log); // logs "Cancelled" at t=50ms
 */

export { cancellable }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy