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

Factory methods for Promise creation #104

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

njannink
Copy link

I added some factory methods to create the Promises. This prevents the need of casts in some Then(...) constructions:

old
.Then(() => (IPromise) new Promise((resolve, reject) => ....))

new
.Then(() => Promise.Create((resolve, reject) => ....))

@RoryDungan
Copy link
Contributor

Looks good although could do with some unit tests and I'm not entirely sure if I understand the use-case for this. In the example you gave with a cast to an IPromise, the cast doesn't appear to do anything. It shouldn't matter in that case whether it's an IPromise or Promise because the chain of .Thens won't continue until the promise returned by the .Then callback has been resolved anyway. Also since Promise already implements IPromise there should never be any need to cast it to an IPromise. Is there something I'm missing here?

@njannink
Copy link
Author

njannink commented Oct 23, 2019

The problem is with IPromiseOfT. If you want to convert that one to a Promise that doesn't do anything you run into problems at this moment if you want to convert that one in a Promise with no result. You then get a IPromise<Promise>

            IPromise promise = new Promise();

            IPromise<bool> boolPromise = promise.Then(() => new Promise<bool>());

            IPromise<Promise> wrapped = boolPromise.Then(b => new Promise((resolve, reject) =>
            {
                if (b)
                    resolve();
                else
                    reject(new Exception());
            }));

            IPromise good = boolPromise.Then(b => Promise.Create((resolve, reject) =>
            {
                if (b)
                    resolve();
                else
                    reject(new Exception());
            }));

I will add some tests

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.

2 participants