正在编写 async/await 版本的回调

Writing async/await version of a callback

我正在尝试将 callback 重写为 async/await,但以下代码不起作用,导致注释行的 CPU 偏高。

const kurento = require('kurento-client')

function getKurentoClient (kmsUrl) {
  return new Promise((resolve, reject) => {
    kurento(kmsUrl, (error, kurentoClient) => {
      if (error) {
        return reject(error)
      } else {
        return resolve(kurentoClient) // never returns
      }
    })
  })
}

async function test () {
  let result = await getKurentoClient('ws://localhost:8888/kurento')
  console.log('done')
}

test()

来自mozilla.org website

The Promise.resolve(value) method returns a Promise object that is resolved with the given value. If the value is a thenable (i.e. has a "then" method), the returned promise will "follow" that thenable, adopting its eventual state; if the value was a promise, that object becomes the result of the call to Promise.resolve; otherwise the returned promise will be fulfilled with the value.

来自 bluebird GitHub:

when promises resolve other promises or things with then(aka thenables, like _kurentoClient in this case) - they have to wait for it to call then itself.

如您所料,kurento 客户端有一个 then 函数(thenable 也是如此),因此 Promise 正在尝试解决它。由于错误(或不是错误。老实说,我还没有进行足够的研究来确定)它会永远解决自己。

据我所知,似乎 this kurento commit 试图修复它,解析为 "untheanble"。我看到提交来自 31 Oct 2016 但最新版本来自 Sep 2016 所以我认为未部署固定版本。