Javascript 无需等待就解决承诺

Javascript 无需等待就解决承诺,javascript,typescript,promise,Javascript,Typescript,Promise,是否有可能在没有等待功能的情况下解决承诺?我已经读过使用setTimeout函数是可能的,但是我不知道需要多长时间才能响应。当我写到console.log时,它似乎在没有等待函数的情况下也能工作,我想知道它在console.log中是如何工作的,但在分配结果时却没有 const shareName="myshare"; const shareClient = serviceClient.getShareClient(shareName); let shareExists:bo

是否有可能在没有等待功能的情况下解决承诺?我已经读过使用setTimeout函数是可能的,但是我不知道需要多长时间才能响应。当我写到console.log时,它似乎在没有等待函数的情况下也能工作,我想知道它在console.log中是如何工作的,但在分配结果时却没有

const shareName="myshare";
const shareClient = serviceClient.getShareClient(shareName);
let shareExists:boolean;
//The below line doesn't return the result if I don't use await and shareExists will be undefined
shareClient.exists().then((result)=>{shareExists = result;}).catch((error)=>console.log(error.message));

//But when I write to console.log, it works fine without await, I can see the result in console
shareClient.exists().then((result)=>console.log(result)).catch((error)=>console.log(error.message))
我在解决foreach循环中的结果时也遇到了问题。在then函数中,我试图连接上传的文件,但得到的是空文件列表

let fileList = "";
files.forEach(file => {
    let bufferContent = Buffer.from(file.fileContent,'base64');
    fileClient.uploadData(bufferContent)
        .then(()=>{fileList+=file.fileName;fileList += ";"})
        .catch((error)=>{this.showError(error,"processFile")});
});
我尝试添加async/Wait,如下所示,但行为相同

files.forEach(async file => {
    let bufferContent = Buffer.from(file.fileContent,'base64');
    await fileClient.uploadData(bufferContent)
        .then(()=>{fileList+=file.fileName;fileList += ";"})
        .catch((error)=>{this.showError(error,"processFile")});
});

谁能帮帮我吗?首先感谢您,您误用了wait。你不用等待和等待。然后。使用wait代替。然后。其次,第一段代码中的注释没有意义。您一直在谈论wait,但没有在代码中使用它。此外,console.log无法向您显示值,但它不适用于分配给shareExists。那里有点不对劲。最后,wait和.then在功能上完全相同。等待只是“语法糖”来代替。所以你可以互换使用。谢谢@seesharper的回复。请原谅我的基本知识,因为我不熟悉打字和承诺。这就是我试过的<代码>导出异步函数main(){shareClient.exists().then((结果)=>{shareExists=result;}).catch((错误)=>console.log(error.message));console.log(“之后:“+shareExists”);}当我尝试在不等待的情况下写入shareExists的值时,它将打印未定义,如果我放入wait,则打印良好。但是如果我尝试
shareClient.exists(),那么((结果)=>console.log(结果)).catch((错误)=>console.log(错误消息)),我可以在控制台输出中看到结果,即使没有await@seesharper如果您看到我的问题,在foreach中,我确实尝试过在then函数中无等待地解决承诺。我本来希望“then”函数在处理后更新文件列表,但事实并非如此。我得到了一张空名单,你在滥用等待。你不用等待和等待。然后。使用wait代替。然后。其次,第一段代码中的注释没有意义。您一直在谈论wait,但没有在代码中使用它。此外,console.log无法向您显示值,但它不适用于分配给shareExists。那里有点不对劲。最后,wait和.then在功能上完全相同。等待只是“语法糖”来代替。所以你可以互换使用。谢谢@seesharper的回复。请原谅我的基本知识,因为我不熟悉打字和承诺。这就是我试过的<代码>导出异步函数main(){shareClient.exists().then((结果)=>{shareExists=result;}).catch((错误)=>console.log(error.message));console.log(“之后:“+shareExists”);}
当我尝试在不等待的情况下写入shareExists的值时,它将打印未定义,如果我放入wait,则打印良好。但是如果我尝试
shareClient.exists(),那么((结果)=>console.log(结果)).catch((错误)=>console.log(错误消息)),我可以在控制台输出中看到结果,即使没有await@seesharper如果您看到我的问题,在foreach中,我确实尝试过在then函数中无等待地解决承诺。我本来希望“then”函数在处理后更新文件列表,但事实并非如此。我得到一张空名单