我怎样才能避免在我的案例中使用另一个承诺?

How can I avoid using another promise in my case?

我有两个 .js 文件,例如:

index.js:

app.get("/testing", (req, res) => {
  testAsyncFunction().then((resolvedData) => {
    res.send(resolvedData);
  });
})

和server.js:

const asyncFunction = () => {
  return new Promise((res) => {
    setTimeout(() => {
      res('resolved');
    },3000 )
  })
}

const testAsyncFunction = () => {
  return new Promise(async (res) => {
    const result = await asyncFunction();
    return res(result);
  })
}

这是按预期工作的,但是如果我将 testAsyncFunction(这样我就不会创建新的承诺)更改为如下所示:

const testAsyncFunction = async () => {
  const result = await asyncFunction();  
  return result;
}

并在 index.js 中:

app.get("/testing", (req, res) => {
  res.send(testAsyncFunction());
})

我得到一个空对象,因为它没有等待 3 秒,在后一种情况下我错过了什么?我想避免创建一个新的 Promise 只是为了等待另一个 Promise。

更新

我将 testAsyncFunction 更改为如下内容:

const testAsyncFunction = () => {
  asyncFunction().then((result) => {
    return result;
  })  
}

虽然有上面的功能。不是异步函数,为什么我仍然必须在 index.js 中等待它。我假设返回值在这种情况下不会是一个承诺,所以这是我感到困惑的部分。

so that I don't create a new promise 应该是这样,其他方式就是反模式。然而,当一个函数 returns 一个 promise 你需要等待相同的

app.get("/testing", async (req, res) => {
  let obj = await testAsyncFunction()
  res.send(obj );
});

app.get("/testing", (req, res) => {
  testAsyncFunction().then((obj) => {
    res.send(obj );
  })
});
const testAsyncFunction = async () => {
  const result = await asyncFunction();  
  return result;
}

async 功能总是 return 承诺。所以这相当于:

const testAsyncFunction = () => {
  return asyncFunction();  
}

I want to avoid creating a new Promise just to wait for another promise.

所以只需使用现有的承诺:

app.get("/testing", (req, res) => {
  asyncFunction().then((resolvedData) => {
    res.send(resolvedData);
  });
})

const asyncFunction = () => {
  return new Promise((res) => {
    setTimeout(() => {
      res('resolved');
    },3000 )
  })
}