最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "rejected promise not handled within 1 second" vscode Extension API - Stack Overflow

programmeradmin2浏览0评论

I'm trying to write a simple extension for VS code that renames a selection to a given string. The app was bootstrapped with the extension generator:

For this I use this code:

const editor = vscode.window.activeTextEditor;
    if (!editor) throw Error;

    const position = editor.selection.active
    const uri = editor.document.uri

    vscodemands.executeCommand("vscode.executeDocumentRenameProvider", uri, position, "donkey")
        .then(edit => {
            if (!edit) throw Error;

            return vscode.workspace.applyEdit(edit);
        });

The mand is bound to a keybinding. I launch the debugger with F5(launching an instance of vs code for debugging like in the tutorial: ). I then select a bunch of code in a file that I opened in that debugging instance and press my keybinding.

However, in the Debug Console I get "rejected promise not handled within 1 second". No error is thrown and since executeCommand is a Thenable, not a real Promise, I can't call catch() on it.

I tried to wrap the call in a try/catch block but with no success. When i try other to do other stuff, like showing a message with vscode.window.showInformationMessage or prompt the user for input it works and I don't see the error.

I also tried to do the same with a Typescript version of the extension, but I get the same behavior.

I don't see what I am doing wrong, is there something I am missing ?

I'm trying to write a simple extension for VS code that renames a selection to a given string. The app was bootstrapped with the extension generator: https://code.visualstudio./docs/extensions/example-hello-world#_generate-a-new-extension

For this I use this code:

const editor = vscode.window.activeTextEditor;
    if (!editor) throw Error;

    const position = editor.selection.active
    const uri = editor.document.uri

    vscode.mands.executeCommand("vscode.executeDocumentRenameProvider", uri, position, "donkey")
        .then(edit => {
            if (!edit) throw Error;

            return vscode.workspace.applyEdit(edit);
        });

The mand is bound to a keybinding. I launch the debugger with F5(launching an instance of vs code for debugging like in the tutorial: https://code.visualstudio./docs/extensions/example-hello-world#_debugging-your-extension ). I then select a bunch of code in a file that I opened in that debugging instance and press my keybinding.

However, in the Debug Console I get "rejected promise not handled within 1 second". No error is thrown and since executeCommand is a Thenable, not a real Promise, I can't call catch() on it.

I tried to wrap the call in a try/catch block but with no success. When i try other to do other stuff, like showing a message with vscode.window.showInformationMessage or prompt the user for input it works and I don't see the error.

I also tried to do the same with a Typescript version of the extension, but I get the same behavior.

I don't see what I am doing wrong, is there something I am missing ?

Share Improve this question asked May 1, 2018 at 6:32 Antha On TwitchAntha On Twitch 931 gold badge1 silver badge4 bronze badges 3
  • If it's not currently a real Promise, you could convert it to one yourself and catch, right? – CertainPerformance Commented May 1, 2018 at 6:34
  • Have you try not a try/catch, but catch()? – kshetline Commented May 1, 2018 at 6:38
  • Yep, I still get the same result. I read that this can be caused by promises failing without calling reject() or something, however this es from the vscode api function itself. – Antha On Twitch Commented May 1, 2018 at 6:44
Add a ment  | 

1 Answer 1

Reset to default 6

Thenable.then takes two arguments: a success continuation and a failure continuation. You can use the failure continuation to make sure rejections are properly handled:

vscode.mands.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');
    })

This way, if executeCommand, the previous then, or applyEdit fail, the rejection is properly handled

发布评论

评论列表(0)

  1. 暂无评论