【问题标题】:How to resolve [object Promise] issue in visual studio code extension.js如何解决 Visual Studio 代码 extension.js 中的 [object Promise] 问题
【发布时间】:2019-09-06 00:36:56
【问题描述】:

试图在 VS 代码 extension.js 中获取当前项目文件夹路径,但我收到 [object Promise] 错误。如何解决这个问题?谁能解决这个问题?

extension.js:

const getCurrentlyopenedprojectpath = async() => {

  const editor = window.activeTextEditor;
  if (!editor || !workspace.workspaceFolders || workspace.workspaceFolders.length < 2) {
    return null;
  }
  let text;
  const resource = editor.document.uri;
  if (resource.scheme === 'file') {
    const folder = workspace.getWorkspaceFolder(resource);
    if (!folder) {
      text = `$(alert) <outside workspace> → ${basename(resource.fsPath)}`;
    } else {
      text = `$(file-submodule) ${basename(folder.uri.fsPath)} (${folder.index + 1} of ${workspace.workspaceFolders.length}) → $(file-code) ${basename(resource.fsPath)}`;

    }
  }
  return {
    text
  };
}


console.log(getCurrentlyopenedprojectpath());

【问题讨论】:

    标签: javascript node.js visual-studio-code


    【解决方案1】:

    您需要等待您的来电。异步函数返回承诺。然后,您可以在解析时使用 then 来获取数据,也可以使用 await(如果代码在异步函数中)。

    由于您在全局范围内使用调用,因此您需要使用then() 来捕获解析的数据。

    getCurrentlyopenedprojectpath().then(result => {
      console.log(result.text)
    })
    

    【讨论】:

    • 您能说得更具体些吗?它怎么不工作?
    • 没有打印
    • 获取结果为空
    【解决方案2】:

    我在您的代码中没有看到任何异步请求,因此只需从您的函数中删除异步即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-12
      • 2019-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      相关资源
      最近更新 更多