如何使用async / await语法返回诺言?

So I am writing a function to upload a file and I am using a class Upload to do so. This class has a method called start that basically uploads the file to where I need it and return an object with the info of the file uploaded.

我所做的是:

upload = async (param, onFinish) => {
  const upload = new Upload(param)
  let response = await upload.start()

  response = doSomeWork(response)

  onFinish(response)
}

我这样使用它:

upload(param, (response) => {console.log(response)} )

如您所见,我几乎没有对javascript中的异步函数感到困惑,我可以看到它看起来很奇怪,原因之一是因为我到处都读到异步函数应始终返回promise。

但是我的问题是,在这种情况下,如果我需要首先在响应中做一些工作,我该如何返回承诺?实现我的异步功能可靠的最佳方法是什么?

我已经搜索了堆栈溢出问题,但没有为我的问题找到明确的答案,我不知道是否是因为我没有正确理解答案,或者因为实际上没有答案,但是我希望这不是重复的问题。

评论