Comments (3)
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.
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);
});
from c-sharp-promise.
@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)
- Feature Request: Promise retry HOT 2
- How to exit from then when check some conditions HOT 6
- Not Usable inside Tasks? HOT 3
- NuGet Package outdated since last improvements
- Cancelling a promise. HOT 1
- Default and custom error handler HOT 1
- GC caused by Closure HOT 3
- OpenUPM support. Unity Package Manager HOT 3
- A unsolved ambiguous compile error
- Compile Errors in Unity 2020.2.6f1 HOT 1
- For a complete noob... how and what do I import to my Unity project to start using this? HOT 1
- Different results for javascript.Promise and C-Sharp-Promise HOT 2
- Support .NET Standard and 3.5 into the same Unity project HOT 1
- Some general questions about handling promises HOT 3
- Promises are not Serializable HOT 1
- Need implicit casts between Promise<object> and Promise and interfaces HOT 3
- Catch from a Promise<object> returns IPromise instead of IPromise<object> HOT 2
- Promise Queue? HOT 2
- How can I get response data at the first stage HOT 7
- Create an Unity Package of this library HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from c-sharp-promise.