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

Allow resolving with a custom value #25

Merged
merged 6 commits into from
Apr 23, 2022

Conversation

Richienb
Copy link
Contributor

@Richienb Richienb commented Apr 6, 2022

Fixes #12
Fixes #22
Fixes #23

Signed-off-by: Richie Bendall <richiebendall@gmail.com>
@Richienb Richienb changed the title Allow resolving with custom value Allow resolving with a custom value Apr 6, 2022
index.d.ts Outdated Show resolved Hide resolved
Signed-off-by: Richie Bendall <richiebendall@gmail.com>
readme.md Outdated Show resolved Hide resolved
Signed-off-by: Richie Bendall <richiebendall@gmail.com>
index.d.ts Outdated Show resolved Hide resolved
Signed-off-by: Richie Bendall <richiebendall@gmail.com>
@sindresorhus
Copy link
Owner

@Richienb The method should be attached to the method, as discussed in #25 (comment)

Signed-off-by: Richie Bendall <richiebendall@gmail.com>
@Richienb
Copy link
Contributor Author

@sindresorhus

@sindresorhus sindresorhus merged commit 65f3672 into sindresorhus:main Apr 23, 2022
@fregante
Copy link

Not a fan of this, my suggestion used the standard method used by the Promise constructor, without having to reference pWaitFor itself

@leonsilicon
Copy link

I think an issue with using the Promise constructor is the suboptimal support for TypeScript since the types can't be auto-inferred and need to be explicitly specified with the generic argument (which isn't always practical). I believe this is a TypeScript limitation since I don't think there's a way to have the generic parameter auto-inferred in new Promise<T>(...).

@fregante
Copy link

fregante commented Apr 23, 2022

An alternative would be to have an option deciding how to handle the returned value:

  • discard (current, resolves on truthy values)
  • preserve (new, resolves on non-undefined

If you want to really return undefined then you could supply a symbol: return pWaitFor.undefined

The current default where the returned value is ignored is kind of annoying.

@sindresorhus
Copy link
Owner

discard (current, resolves on truthy values)

Boolean, not truthy/falsy.

@sindresorhus
Copy link
Owner

If you want to really return undefined then you could supply a symbol: return pWaitFor.undefined

Would be a strange API.

@sindresorhus
Copy link
Owner

sindresorhus commented Apr 23, 2022

I personally think using a symbol-returning method for this is an elegant solution.

@sindresorhus
Copy link
Owner

This package has existed for years without the direct ability to return a value and you are the only ones that have requested it, so I don't think it's a common need either.

@sindresorhus
Copy link
Owner

We could consider some nice handling for the case of wanting the value when it's not undefined, instead of checking a boolean.

Some ideas:

import pWaitFor from 'p-wait-for';

const path = await pWaitFor.defined(async () => await getPath());

console.log(path);
import pWaitFor from 'p-wait-for';

const path = await pWaitFor(async () => {
	let path = await getPath();
	return pWaitFor.resolveIfDefined(path);
});

console.log(path);

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 this pull request may close these issues.

Resolve with return value
4 participants