sindresorhus/p-wait-for

Resolve with return value

Closed this issue · 4 comments

This should resolve with the return value of the function that returns the truthy value. This is useful for promise-returning functions that return either a value or undefined.

It's expected to return a boolean though, not a truthy value. I'm generally not a fan of truthy checks, as it's impossible to type in TS and can cause bugs. If you need the value, I think we could design something around that.

Maybe:

const pWaitFor = require('p-wait-for');
const pathExists = require('path-exists');

(async () => {
	const path = await pWaitFor.withValue(() => {
		let path = getPath();

		return {
			isTrue: pathExists(path),
			returnValue: path
		};
	});

	console.log(path);
})();

I'm open to suggestions if you can think of something more elegant.

resolve 😅

const pWaitFor = require('p-wait-for');
const pathExists = require('path-exists');

(async () => {
	const path = await pWaitFor((resolve) => {
		let path = getPath();
		if (pathExists(path)) {
			resolve(path)
		}
	});

	console.log(path);
})();

I would also suggest tightening the callback type to ensure only boolean can be returned, or else one might think return ['name'] would be preserved since the types allow for it.

This makes it a little more annoying since you must return false (at least in TS) instead of not returning anything in some cases.

I agree, the types should be made stricter, also the JS validation.