I have a function which return a promise getPort() from the npm package https://www.npmjs.com/package/get-port.
I need to change this one so I can stop the code until I don't have the value. like:
var port = getPortSync()
I can't use generators or await / async cuz the code is actually not in a function.
Basically, I want to do something like readFileSync in node.
The reason I'm doing this is that I have a bunch of mocha tests where the variable are declared on the top of the file, I need to retrieve the free port value before the declaration/ initialization of those variables and I want to change the minimum number of lines.
My current attempt is:
var getPortSync = function() {
var port = null
getPort().then(function(freePort) {
console.log('port', freePort)
port = freePort
})
while (port === null) {
console.log('port: ', port)
}
return port
}
thenof the returned promise? Second, does your attempt work?