I am trying to learn about Promise in clojure. From the docs, I can see we can create promise using promise function:
(def p (promise))
and we can resolve it using deliver function like this
(deliver p 42)
I want to understand, how can we reject a promise with error or exception. I've tried docs but it doesn't seems to help. I am new to functional programming and wondering there could be a different way for failed promises.
You can use fail function to reject a promise.
Please refer to this link for more details.
Related
I've developed a function where i use the return function to spit out a list of vectors. Unfortunately there are still a few bugs in my code. Once my function has failed due to error can i recover that list of vectors?
Functions have their own scope, so if a function fails, the function will exit and no return value will be returned. It's difficult to say what's making your function fail without looking at your code. If the failure is due to something you have control of in your code, I suggest you solve it before relying on potentially function bogus results. But if the failure is out of your control (e.g. calling an unavailable external data source) you can wrap your risky code in a try call to recover in the event of an error. I hope this helps.
As this thread, we can stop iteration loop by setting function (f:trainer -> bool) as Trainer's stop_triger.
But in this way, I think we can't use other extension such as LogReport which use stop_trigger=((args.epoch, '10')).
So, my question is how to implement early stopping as the Extension and how to send a signal to stop trainer's iteration from Extension.
thanks.
I implemented the example code on gist,
and updated the answer on the original thread.
I noticed that stop_trigger originally uses tuple notation like (args.epoch, '10'), instead we need to change to pass a callable object (EarlyStoppingTrigger in above example).
Do we have an abort() function to terminate a XQuery script from XQuery 3.1? I can write a if-else to return nothing. But I dont want to do that.
You can use the error(...) function for that. The thrown error can then be caught by try/catch if needed. Using errors for control flow is not advisable however, because it is normally not optimized for performance.
What is the difference between Meteor.autorun and Tracker.autorun?
are they just aliases?
is one deprecated?
is there any instance where one is preferable to the other?
I'm well aware of the difference in using this.autorun in template lifecycle callbacks, but have seen these two used interchangeably and just want to be sure I haven't missed a trick.
Well, it can easily be found out with the identity operator.
This will be false because it is not the same function:
(function() {} === function() {})
Let's try with the two autorun :
(Meteor.autorun === Tracker.autorun)
This returns true. So yes it's only a pure alias.
However, only Tracker.autorun is documented. I suspect some kind of old API left for compatibility...
Let's check some Meteor code on GitHub!
File : deprecated.js
Meteor.autorun = Tracker.autorun;
This is in deprecated.js, it says some things about //Deprecated functions and some backward compatibility with Meteor 0.5.4. It seems pretty clear which one you should use.
You can find some other old timers in there, such as Deps...
Try to run Meteor.autorun(); in the console, it throws the following error Uncaught Error: Tracker.autorun requires a function argument like you were trying to run Tracker.autorun();
A Promise is an object type which serves as a placeholder for a future result,
such as the body of an HTTP request, or the return value of a Meteor method call.
Basically any function that forces you to pass a callback to recieve its
return value (instead of just returning it) is said to be an async function,
and the value it gives back can be represented by a Promise.
The issue in Meteor is that helper methods are only intended to work with
synchronous values - such as the text in a web page, or the contents of a
Minimongo collection. When you return a Promise from one, the helper
shows [object Promise] instead of the resolved value
does not update when the promise resolves
Some attempts at solving this exist: simple:reactive-method
and arsnebula:reactive-promise, but they require you to change your helpers to a certain style, or only work with Meteor.call instead of just simply allowing a generic promise to be returned.
Is there something existing I've overlooked, or is there a solution in the works? I've been experimenting with this for some time, and may work on something myself if there's not an official answer.
Even with respect to other libraries out there, I think the answer for now is to go with the package deanius:promise (disclaimer: I authored it, with input from the authors of some other packages).
It does what the question asks, and adds some nice touches like controllable error and loading messages.