Giter VIP home page Giter VIP logo

Need implicit casts between Promise<object> and Promise and interfaces about c-sharp-promise HOT 3 CLOSED

ericnewton76 avatar ericnewton76 commented on June 2, 2024
Need implicit casts between Promise and Promise and interfaces

from c-sharp-promise.

Comments (3)

ashleydavis avatar ashleydavis commented on June 2, 2024 1

It doesn't feel right to me that a promise that yields no value should be able to implicitly cast to a null result.

There are two reasons for the interfaces.

  1. Having an interface allows a promise to be mocked.
  2. Having two interfaces allows you to seperate your concerns. One interface allows a promise to be awaited, the other allows a promise to be resolved.

from c-sharp-promise.

ericnewton76 avatar ericnewton76 commented on June 2, 2024

Just to clarify after thinking about this.

All Promises should still shuttle around an object reference, and that object ref can be null.

So a Then((x) => { Assert.That(x==null) }) on a Promise<object> cast to a Promise. This should work fine, given the following:

var Promise1 = new Promise((resolve,reject) => { resolve(); });
var Promise2 = new Promise<string>((resolve,reject) => { resolve("A"); });
var Promise3 = new Promise<int>((resolve,reject) => { resolve(1); });

Promise.All(Promise1, Promise2, Promise3) 
//Could force this to be Promise<object>.All but realistically that should be the default I think
//but even Promise<object>.All doesnt work because Promise1 cant convert to IPromise<object>
.Then(results => {
  Assert.That(results.ElementAt(0), Is.Null);

  Assert.That(results.ElementAt(1), Is.NotNull);
  Assert.That(results.ElementAt(1), Is.EqualTo("A"));

  Assert.That(results.ElementAt(2), Is.NotNull);
  Assert.That(results.ElementAt(2), 1);
}

Unless I'm not thinking about this correctly... lol

I will probably generate a PR for this, I worry that it'll touch every aspect of the library though.

from c-sharp-promise.

ericnewton76 avatar ericnewton76 commented on June 2, 2024

Just a question, was there a need for the IPromise interfaces? I dont actually see a need for them.

That way, Promise.All takes Promise types instead, and the implicit casts can easily shuttle Promise to Promise due to baseclass

public static Promise Promise.All(params Promise[] promises)

from c-sharp-promise.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.