try, catch, finally

45 views
Skip to first unread message

Tomas Neme

unread,
Jun 4, 2013, 1:19:32 PM6/4/13
to q-con...@googlegroups.com
I read this in the docs:

With this approach, you also get implicit error propagation, just like
try, catch, and finally. An error in step1 will flow all the way to
step5, where it’s caught and handled.

and I thought I could do this with a semaphore:

sem.take(function() { callPromise().fin(sem.leave) })

I was expecting that to have the semaphore be released whether there's
an error or not, but to have any unhandled errors thrown anyways, like
if I'd done

try {
} finally {
sem.leave()
}

but while I have the semaphore released, callPromise is failing silently.

How should I have this to release the semaphore regardless of errors,
but still raise any errors?

thanks
tomas

--
"The whole of Japan is pure invention. There is no such country, there
are no such people" --Oscar Wilde

|_|0|_|
|_|_|0|
|0|0|0|

(\__/)
(='.'=)This is Bunny. Copy and paste bunny
(")_(") to help him gain world domination.

Domenic Denicola

unread,
Jun 4, 2013, 11:22:06 PM6/4/13
to q-con...@googlegroups.com
Could you give an example that doesn't involve other libraries, but instead just Q? It would be much easier to diagnose what's going wrong in that case. I'm not familiar with what terms like "sem.take" "semaphore be released", "sem.leave", etc. mean, or how they fit into this sem library you're using, and it would be nicer if we could guarantee it wasn't an issue with the interaction of this library and Q but instead with Q itself.
> --
> You received this message because you are subscribed to the Google Groups
> "Q Continuum (JavaScript)" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to q-continuum...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Kris Kowal

unread,
Jun 4, 2013, 11:31:57 PM6/4/13
to Q Continuum

What is likely happening is that an error is getting swallowed. Try:

sem.take(function() {
    callPromise().fin(sem.leave).done()
})

Also, you’ll find that the require("q/queue") module gives you an infinite promise queue, which could be used as a semaphore.

var Queue = require("q/queue");
var semaphore = Queue();

semaphore.get()
.then(callPromise)
.finally(semaphore.put)
.done();

Kris Kowal

Tomas Neme

unread,
Jun 6, 2013, 8:50:09 AM6/6/13
to q-con...@googlegroups.com
> Also, you’ll find that the require("q/queue") module gives you an infinite
> promise queue, which could be used as a semaphore.
>
> var Queue = require("q/queue");
> var semaphore = Queue();
>
> semaphore.get()
> .then(callPromise)
> .finally(semaphore.put)
> .done();

wow, nice to know.

as for the error I was having, I just figured it was being handled in
ANOTHER .fin() further down the promise chain, sorry
Reply all
Reply to author
Forward
0 new messages