我正在尝试为 VS 代码编写一个简单的扩展,将选择重命名为给定的字符串。该应用程序是使用扩展生成器引导的:https://code.visualstudio.com/docs/extensions/example-hello-world#_generate-a-new-extension
为此我使用了这段代码:
const editor = vscode.window.activeTextEditor;
if (!editor) throw Error;
const position = editor.selection.active
const uri = editor.document.uri
vscode.commands.executeCommand("vscode.executeDocumentRenameProvider", uri, position, "donkey")
.then(edit => {
if (!edit) throw Error;
return vscode.workspace.applyEdit(edit);
});
命令绑定(bind)到一个键绑定(bind)。我使用 F5 启动调试器(启动 vs 代码实例进行调试,如教程中所示:https://code.visualstudio.com/docs/extensions/example-hello-world#_debugging-your-extension)。然后,我在该调试实例中打开的文件中选择一堆代码,然后按下我的键绑定(bind)。
但是,在调试控制台中,我得到“拒绝 promise 未在 1 秒内处理”。没有错误被抛出,因为 executeCommand 是一个 Thenable,而不是一个真正的 Promise,我不能在它上面调用 catch()。
我试图将调用包装在 try/catch block 中,但没有成功。 当我尝试其他人做其他事情时,例如使用 vscode.window.showInformationMessage 显示消息或提示用户输入它有效,但我没有看到错误。
我也尝试对扩展的 Typescript 版本执行相同的操作,但我得到了相同的行为。
我看不出我做错了什么,是不是我遗漏了什么?
最佳答案
Thenable.then
接受两个参数:成功延续和失败延续。您可以使用失败延续来确保正确处理拒绝:
vscode.commands.executeCommand("vscode.executeDocumentRenameProvider", uri, position, "donkey")
.then(edit => {
if (!edit) throw Error;
return vscode.workspace.applyEdit(edit);
})
.then(undefined, err => {
console.error('I am error');
})
这样,如果executeCommand
,前面的then
,或者applyEdit
都失败了,rejection就被妥善处理了
关于javascript - "rejected promise not handled within 1 second"vscode 扩展 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50112269/