-
Notifications
You must be signed in to change notification settings - Fork 58
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
AsyncEither example #16
Comments
The best way to think about The As a simple example: function request(url) {
return AsyncEither( fetch(url) )
.chain(resp => (
!resp.ok ?
AsyncEither.Left("Request failed.") :
AsyncEither.Right(resp.json())
));
}
// later
var records = await (
request("/some-api")
.map(data => data.records)
.fold(
(err) => { logError(err); return []; },
v => v
)
); Like promises (and I've included But in truth, I think all I/O (like a But you can think of Hope that's helpful! |
Ty for you answer Kyle,
But it seems to report some internal error:
|
For posterity, function request(url) {
return AsyncEither( fetch(url) )
.map(resp => {
if ( !resp.ok ) throw "Request failed.";
else return resp.json();
});
} This style is a little more familiar/ergonomic to JS devs, but it relies on conveniences that |
Are you using Monio off npm, or from github? I'm not sure, but... I think this may be a bug that I've fixed in the code that's on github, but hasn't yet been released to npm. That release is coming shortly I believe. |
Yeah I am using it of npm probably still isn't fixed, I thought I was doing something wrong. Ty for your help |
Hey sorry to bother u again Kyle, I used "IO.do" syntax and there I could use try/catch to catch an exception, but I don't know how i can catch exception when I am using more "functional" syntax. This is the example I tried: const sendRequest = url => IO.of(fetch(url).catch(err => {
console.log("ERROR: " + err);
return err}));
const getJson = response => IO.of(response.json());
sendRequest('https://swapi.dev/api/WrongUrl')
.chain(getJson)
.map(x => console.log('SUCCESS: ' + x))
.run(); |
Try this: const sendRequest = url => IO.of(fetch(url));
const getJson = response => IO.of(response.json());
sendRequest('https://swapi.dev/api/WrongUrl')
.chain(getJson)
.map(x => console.log('SUCCESS: ' + x))
.run()
// we get a promise back here, so we can call `catch(..)` on it
.catch(err => console.log("ERROR:",err)); |
Yeah that works, |
Just use |
Hello Kyle,
I am having trouble understanding and using AsyncEither, can you give some kind of example of usage?
The text was updated successfully, but these errors were encountered: