如何组合可P-Cantoleable和P-Progress NPM软件包以获得可取消的进度报告承诺
我想有一个可取消的承诺,同时报告报告。类似于 p-progress 和 p-cancelable 。尽管它们中的任何一个都很简单,但我正在努力将它们兼而有之。
这是我到目前为止尝试的方法,它成功地报告了进度,但抛出custablePromise.cancel不是函数
错误。
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);
}
});
});
}
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);
}
});
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了使这个完成 - 我最终以@bergi的建议,使用不同的方法,使用
abortController
并扩展了eventEmitter
class(我正在nodejs中编码 - 用于编码 - 您可以继承eventtarget
)。也许有人会对回答原来的问题感兴趣 - 尝试结合两个库时,我最终创建了一个新的
casufableprogressprogrespromise
类,并从p-progress中复制最必要的部分
库并扩展PCANCELABLE
:用法示例:
To make this complete - I ended up, as @Bergi suggested, with a different approach i.e., using
AbortController
and extending theEventEmitter
class (I'm coding in NodeJS - for the web you can inheritEventTarget
).Maybe someone will be interested in answer to the original question - when trying to combine the two libraries I ended up creating a new
CancelableProgressPromise
class and copy-pasting the most necessary parts from thep-progress
library to it and extending thePCancelable
:Usage example: