Instead of checking if a file exists, we just try to open it.
So, instead of searching for a port, try to use the port and try another if it fails.
So, for example:
import express from 'express';
import {getValidatedPort} from 'get-port';
import pDefer from 'p-defer';
const app = express();
function listen(port) {
const {promise, resolve, reject} = pDefer();
app
.listen(port, () => {
app.off('error', reject);
resolve();
})
.on('error', reject);
return promise;
}
await getValidatedPort(listen);
What should the getValidatedPort function return? If we return the port that is used, we can't access the return value of the validation function and vice versa.
How should we check if the validator function approves an option? In the example, we check for a resolved or rejected promise but we could instead be checking for a boolean value.
Instead of checking if a file exists, we just try to open it.
So, instead of searching for a port, try to use the port and try another if it fails.
So, for example:
What should the
getValidatedPortfunction return? If we return the port that is used, we can't access the return value of the validation function and vice versa.How should we check if the validator function approves an option? In the example, we check for a resolved or rejected promise but we could instead be checking for a boolean value.