Giter Club home page Giter Club logo

Comments (3)

RoryDungan avatar RoryDungan commented on August 15, 2024 1

Hey!

I can see how that could be useful, but is there a reason your example has a function that can both be passed a callback and return a promise as opposed to just overloading the function and providing two implementations?

Having a single method that contains two code paths to work in two different ways is generally not a good practice in C#. If you want your API to be able to be used with both promises and callbacks, you should be able to accomplish the same thing with overloaded methods like this:

// Method returning a promise
public IPromise<T> Get<T>(string url) 
{
    var promise = new Promise<T>();
    AsyncOperation<T>(url, (err, res) => {
        if (err != null)
        {
            promise.Reject(err);
        }
        else
        {
            promise.Resolve(res);
        }
    });
    return promise;
}

// Method that lets us use a callback
public void Get<T>(string url, Action<Exception, T> callback)
{
    Get(url)
        .Then(value => callback(null, value))
        .Catch(ex => callback(ex, null));
}

The functionality for that code example is demonstrated in the Creating a promise for an Async operation and Waiting for an Async operation to complete examples. Let me know if there's a reason that doesn't work for your use case or you think there's a way we could improve the documentation to be more clear though.

from c-sharp-promise.

jdnichollsc avatar jdnichollsc commented on August 15, 2024

Method of Example

public IPromise<T> Get<T>(string url, Action<Exception, T> callback = null){
  var promise = new Promise<T>();
  AsyncOperation<T>(url, (err, res) => {
    if(err != null){
      promise.Reject(err);
    }
    else{
      promise.Resolve(res);
    }
    if(callback != null) callback(err, res);
  });
  return promise;
}

Examples

//Callbacks
Get<string>("www.nicholls.co/hello", (errHello, resHello) => {
  Console.Write(resHello); //HELLO
  Get<string>("www.nicholls.co/world", (errWorld, resWorld) => {
    Console.Write(" " + resWorld); //HELLO WORLD
  });
});
//Promises
Get<string>("www.nicholls.co/hello").Then(res => {
  Console.Write(res); //HELLO
  return Get<string>("www.nicholls.co/world");
}).Then(res => {
  Console.Write(" " + res); //HELLO WORLD
}).Catch(err => {
  Console.Write(err.Message);
});

screen shot 2017-09-13 at 10 43 01 am

from c-sharp-promise.

jdnichollsc avatar jdnichollsc commented on August 15, 2024

@RoryDungan No, I think that the documentation is excellent, I congratulate you for your great work!

I mean like JS with a lot of APIs that support promises and callbacks at the same time, for example http://slides.com/juandavidnicholls/promises#/3 Maybe it's a good option if you want to reuse the code and have only one method that support both, but is an excellent option with overloaded methods.

Again, I congratulate you on this brilliant idea!

Regards, Nicholls

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.