Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve with return value #12

Closed
Richienb opened this issue Dec 31, 2020 · 4 comments · Fixed by #25
Closed

Resolve with return value #12

Richienb opened this issue Dec 31, 2020 · 4 comments · Fixed by #25

Comments

@Richienb
Copy link
Contributor

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.

@sindresorhus
Copy link
Owner

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.

@fregante
Copy link

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);
})();

@fregante
Copy link

fregante commented Jan 19, 2022

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.

@sindresorhus
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants