4

I'm trying to rewrite a callback as async/await but following code doesn't work and results in high CPU at the commented line.

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()
Xaqron
  • 29,931
  • 42
  • 140
  • 205
  • Possible duplicate of [How do I convert an existing callback API to promises?](https://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises) – Kevin Burdett Oct 26 '17 at 22:34
  • @KevinBurdett: For sure I have tried every available solution in SO without any chance, maybe missing an important point. – Xaqron Oct 26 '17 at 22:36
  • gotcha, I flagged it due to work related-reflex :) The code looks sound to me. I always recommend using [promisify from the Node.JS API](https://nodejs.org/api/util.html#util_util_promisify_original) in place of an instantiated promise. However, the code looks sound. I would start looking at the kurento client/server setup (which I know nothing about). – Kevin Burdett Oct 26 '17 at 22:50
  • do you have a functional sample using callbacks? perhaps something important was lost in the translation? – Kevin Burdett Oct 26 '17 at 22:51
  • @KevinBurdett: The `callback` version works fine. I get `kurentoClient` at the commented line (using breakpoint, it is there after a while) but code execution ends there and nothing returns. – Xaqron Oct 26 '17 at 22:56

1 Answers1

3

From the 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.

And from 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.

As you have guessed, kurento clients have a then function (so is a thenable) so the Promise is trying to resolve it. Because of a bug (or not a bug. Honestly, I have not researched enough to determine that) it keeps resolving itself forever.

As far as I see, seems that this kurento commit tries to fix it, resolving to an "untheanble". I see that the commit is from 31 Oct 2016 but the latest release is from Sep 2016 so I think that the fixed version is not deployed.

Jorge Fuentes González
  • 11,568
  • 4
  • 44
  • 64