Function: withProgress()
withProgress<
R
>(options
,task
):Promise
<R
>
Defined in: packages/extension-api/src/extension-api.d.ts:2259
在 Podman Desktop 中显示进度。在运行给定的回调函数期间以及在其返回的 Promise 未解决或拒绝之前,都会显示进度。进度应显示的位置(以及其他详细信息)通过传递的 ProgressOptions
定义。
类型参数
R
R
参数
options
任务的详细信息选项
task
(progress
, token
) => Promise
<R
>
返回 Promise 的回调函数。可以使用提供的 Progress 对象报告进度状态。
要报告离散进度,请使用 increment
来指示已完成的工作量。每次调用 increment
值时,都会将它们相加并反映为总体进度,直到达到 100%(例如,10
的值代表已完成 10%
的工作)。请注意,目前只有 ProgressLocation.Notification
能够显示离散进度。
要监控操作是否已被用户取消,请使用提供的 CancellationToken
。请注意,目前只有 ProgressLocation.Notification
支持显示取消按钮来取消长时间运行的操作。
返回
Promise
<R
>
任务回调返回的 Promise。
示例
这是一个简单的例子
await window.withProgress({ location: ProgressLocation.TASK_WIDGET, title: `Running task` },
async progress => {
progress.report({message: 'task1' });
await task1();
progress.report({increment: 50, message: 'task2' });
await task2();
},
);
如果任务回调内部抛出错误,该错误将被传播。
try {
await window.withProgress(
{ location: ProgressLocation.TASK_WIDGET, title: `Running task` },
async progress => {
throw new Error('error when running a task');
},
);
} catch (error) {
// do something with the error
}
您可以从任务回调中返回值
const result: number = await window.withProgress<number>(
{ location: ProgressLocation.TASK_WIDGET, title: `Running task` },
async progress => {
// compute something
return 55;
},
);