Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
How to combine p-cancelable and p-progress npm packages to get a cancelable progress reporting promise

I would like to have a cancelable promise that reports progress at the same time. Something like a combination of p-progress and p-cancelable. And while usage of any of them separately is simple I'm struggling a bit to combine them both.

This is what I tried so far which successfully reports progress but throws cancelablePromise.cancel is not a function error.

import PCancelable from 'p-cancelable';
import PProgress  from 'p-progress';

const cancelablePromise = doJobWithProgress();

try {
    cancelablePromise.onProgress((progress) => {
        console.log('progress: ' + progress);
    });

    await sleep(500);

    cancelablePromise.cancel();

    const result = await cancelablePromise;

    console.log("result", result);
}
catch (error) {
    console.log("Main catch error: " + error);
}

function sleep(ms) {
    return new Promise((resolve) => {
        setTimeout(resolve, ms);
    });
}

function doJobWithProgress() {
    return PProgress((progressCallback) => {
        return new PCancelable((resolve, reject, onCancel) => {
            try {
                let progress = 0;

                const interval = setInterval(() => {
                    progress += 0.1;
                    progressCallback(progress);
                }, 100);

                const timeout = setTimeout(() => {
                    const result = 1;
                    clearTimeout(timeout);
                    clearInterval(interval);
                    resolve(result);
                }, 1000);
    
                onCancel(() => {
                    console.log('canceled');
                    clearTimeout(timeout);
                    clearInterval(interval);
                });
            }
            catch (error) {
                console.log('Promise catch error: ' + error);
                reject(error);
            }
        });
    });
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!