Node.js 如何修复nodejs上的异步等待逻辑

Node.js 如何修复nodejs上的异步等待逻辑,node.js,google-api-nodejs-client,Node.js,Google Api Nodejs Client,如果google bucket不存在,我将尝试创建一个google bucket,其逻辑如下: async function createBucket(id){ const bucket = storage.bucket("bucket-" + id); const exists = await bucket.exists(); if (!exists) { console.log("creating bucket>>

如果google bucket不存在,我将尝试创建一个google bucket,其逻辑如下:

async function createBucket(id){
    const bucket = storage.bucket("bucket-" + id);
    const exists = await bucket.exists();
    if (!exists) {
      console.log("creating bucket>>" + "bucket-" + id);
      try {
        await bucket.create();
      } catch (e) {
        console.error("error in creating bucket>>>", e);
      }
    } else {
      console.log("it already exists >>" + exists);
    }
}

奇怪的是,我看到
它已经存在>>假
,而我应该只看到
它已经存在>>真
创建bucket>>bucket-123
。如果您能帮助解决此问题,我们将不胜感激。谢谢大家!

问题是
bucket.exists
返回一个带有结果的数组。在我的例子中,
[false]
,这里似乎没有建议这样做。以下内容解决了此问题:

async function createBucket(id){
    const bucket = storage.bucket("bucket-" + id);
    const [exists] = await bucket.exists();
    if (!exists) {
      console.log("creating bucket>>" + "bucket-" + id);
      try {
        await bucket.create();
      } catch (e) {
        console.error("error in creating bucket>>>", e);
      }
    } else {
      console.log("it already exists >>" + exists);
    }
}

如何初始化
存储
?为什么不使用promise bucket.exits.then(…这可能取决于竞争条件。
storage.bucket(“bucket-”+id);
也是一个异步调用?如果编写
wait storage.bucket(“bucket-”+id);
,会发生什么?