Skip to content

Allow a validation function #60

Description

@Richienb

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions