Skip to content
New issue

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

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

Already on GitHub? # to your account

ObservableInput<T> should support Thennable<T>, not PromiseLike<T> #7437

Open
benlesh opened this issue Feb 1, 2024 · 1 comment
Open

ObservableInput<T> should support Thennable<T>, not PromiseLike<T> #7437

benlesh opened this issue Feb 1, 2024 · 1 comment

Comments

@benlesh
Copy link
Member

benlesh commented Feb 1, 2024

The type on ObservableInput is probably too strict.

We'll want to support Thennable<T> instead, as that's all that is required to convert with from et al.

interface Thennable<T> {
  then(onresolved?: ((value: T) => void) | null | undefined, onrejected?: ((reason: any) => void) | null | undefined): void;
}

The upside is we can then support more. The downside is if we decide we need to rely on the returned value of the then function, It'll require a breaking change.

Implementor note: We probably don't want to publicly expose Thennable, I'm sure a lot of folks already have that implemented in their codebase. It's been a mistake that we export * from types, honestly.

@HirparaSEdit
Copy link

// Define the Thennable interface
interface Thennable {
then(onresolved?: ((value: T) => void) | null | undefined, onrejected?: ((reason: any) => void) | null | undefined): void;
}

// Function that takes an ObservableInput
function processObservable(observable: Thennable) {
// Simulate processing the observable
observable.then(
(value) => {
console.log("Resolved:", value);
},
(reason) => {
console.error("Rejected:", reason);
}
);
}

// Example usage
const myObservable: Thennable = {
then: (onresolved, onrejected) => {
// Simulate resolving the observable after a delay
setTimeout(() => {
if (Math.random() < 0.5) {
onresolved?.("Success");
} else {
onrejected?.("Failure");
}
}, 1000);
},
};

// Process the observable
processObservable(myObservable);

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

No branches or pull requests

2 participants