Javascript 等待承诺而不继续执行

Javascript 等待承诺而不继续执行,javascript,promise,async-await,Javascript,Promise,Async Await,我有一部分代码来检索连接到PC的硬件设备。我还使用第三方库来检索这些设备。我是这样做的: console.log("before"); // some code here (async () => { await 3dpartlibrary.getDevices().then(function (myDevices) { for (var i = 0; i < myDevices.length; i++) { console.log(myDevices[

我有一部分代码来检索连接到PC的硬件设备。我还使用第三方库来检索这些设备。我是这样做的:

console.log("before");
// some code here 
(async () => {
  await 3dpartlibrary.getDevices().then(function (myDevices) {  
    for (var i = 0; i < myDevices.length; i++) {
      console.log(myDevices[i]); // i need this information to continue execution
    }
  });
})();
// here i would have a list of devices and i choose one from the list
console.log("after");
console.log(“之前”);
//这里有一些代码
(异步()=>{
等待3dpartlibrary.getDevices()。然后(函数(myDevices){
对于(var i=0;i
但执行仍在继续,一段时间后,我收到了控制台消息。 实际上,控制台中有消息:before、after和devices

我以这种方式放置async,因为它不能放置在函数的顶部

可能异步等待承诺要解决,但下面的代码正在进行中,我希望在转到console.log(“after”)点之前获得我的列表


在继续执行之前,我如何才能等待设备列表?

围绕匿名异步函数调用的代码是什么?“如何在执行该代码之前停止执行?”将所有剩余代码放在
wait…
语句之后。现在的
async/await
部分是无用的。您的代码的行为就像您刚刚编写了
3dpartlibrary.getDevices()。然后(函数(myDevices){…})
。您需要的可能是:
(async()=>{const myDevices=wait 3dpartlibrary.getDevices();for(…){…};/*所有其他代码*/})()您不能停止执行。这感觉像是一场灾难。你想达到什么目的?@3limin4t0r我必须等待一份要使用的设备列表underneath@52d6c6af它的顺序代码,int,在继续之前,我会从客户端系统中获得一个设备列表,虽然这样更好,但我认为它不能解决OP的问题。这将等待
3dpartlibrary.getDevices()
,这不是他想要的吗?请注意,这将
console.log
传递给
forEach()
回调的索引和数组参数。它会的,但这是他们的代码已经在做的事情(以一种不太优雅的方式)。我假设在异步函数之后还有其他代码应该“等待”。@Patrick True,但我猜他使用console.log只是为了测试,他会调用一个函数。
(async () => {
  let myDevices = await 3dpartlibrary.getDevices() 
  myDevices.forEach(console.log)
})();