如何让 .map 在 javascript 中返回结果之前完成?

How to get .map to complete before returning result in javascript?

我正在努力使用 .map 和 await。如何让下面的内容等待地图完成?

export async function buildDeviceArray(userIdList){ //userIdList is an array of ids
await userIdList.map(myFunction2)
return deviceArray1 // returns straight away, rather than waiting for the .map to complete

async function myFunction2(item) {
    let user =  await wixData.get("OurUsers", item)
    let device = await wixData.get("Devices", user.deviceDefault)
    let output =  { "label": device.deviceReference, "value": user.deviceDefault }
    if (user.deviceDefault) deviceArray1.push(output)
    console.log(deviceArray1);
    return
}

这是一个如何将 .map 与异步回调函数结合使用的示例。

var originalArray = [1,2,3,4];

async function multiplyBy2(x) {
  await new Promise(r => setTimeout(r, 5000)); // half second
  return 2*x;
}

async function run() {
  var promiseArray = originalArray.map(multiplyBy2)
  var answerArray = await Promise.all(promiseArray);
  console.log(answerArray);
}

run();