Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何解决visual studio code extension.js中的[对象承诺]问题_Javascript_Node.js_Visual Studio Code - Fatal编程技术网

Javascript 如何解决visual studio code extension.js中的[对象承诺]问题

Javascript 如何解决visual studio code extension.js中的[对象承诺]问题,javascript,node.js,visual-studio-code,Javascript,Node.js,Visual Studio Code,试图在VS code extension.js中获取当前项目文件夹路径,但我得到[object Promise]错误。如何解决这个问题?谁能解决这个问题 extension.js: const getCurrentlyopenedprojectpath = async() => { const editor = window.activeTextEditor; if (!editor || !workspace.workspaceFolders || workspace.work

试图在VS code 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());
const getCurrentlyopenedprojectpath=async()=>{
常量编辑器=window.activeTextEditor;
如果(!editor | | |!workspace.workspaceFolders | | workspace.workspaceFolders.length<2){
返回null;
}
让文字;
const resource=editor.document.uri;
if(resource.scheme==='file'){
const folder=workspace.getWorkspaceFolder(资源);
如果(!文件夹){
text=`$(警报)→ ${basename(resource.fsPath)}`;
}否则{
text=`$(文件子模块)${basename(folder.uri.fsPath)}(${workspace.workspaceFolders.length}的${folder.index+1})→ $(文件代码)${basename(resource.fsPath)}`;
}
}
返回{
文本
};
}
log(getCurrentlyopenedprojectpath());

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

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

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

我在您的代码中看不到任何异步请求,所以只需从函数中删除异步即可。

您能更具体一点吗?它是怎么工作的?没有什么是优先的获取结果是空的