Skip to content
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

Hang when exception thrown in chain #156

Closed
ron-liu opened this issue Sep 25, 2017 · 3 comments
Closed

Hang when exception thrown in chain #156

ron-liu opened this issue Sep 25, 2017 · 3 comments

Comments

@ron-liu
Copy link

ron-liu commented Sep 25, 2017

When there are some exceptions thrown in chain, it will not reject or resolve, it just hang without any helpful information.

Steps to reproduce

Given the following example, it didn't resolve or reject till timeout.

import {task, of} from 'folktale/concurrency/task'
test.only('exception', async () => {
	await task(({resolve, reject}) => setTimeout( ()=>resolve(1) , 10))
	.chain(x=> {
		throw 'error' // emulate some errors
		return of(2)
	})
	.run()
	.promise()
})
// Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

Expected behaviour

I hope when exception thrown, it shall reject automatically.

Environment

  • OS: macOS 10.12.6
  • JavaScript VM: 8
  • Folktale version: 2

Additional information

The following test will be rejected straight away

import {task, of} from 'folktale/concurrency/task'
test.only('exception', async () => {
	await of(1)
	.chain(x=> {
		throw 'error' // emulate some errors by typo or something
		return of(2)
	})
	.run()
	.promise()
})
@robotlolita
Copy link
Member

Ah, yes, unlike Promises, Task won't catch errors for you automatically. You need to wrap those parts in a try/catch and handle them accordingly. I just realised this isn't documented, but it's a deliberate design decision.

In general, I believe that if some exceptional condition that you weren't expecting happens at runtime, your program should crash, then restart in a clean, well-known state. If your program continues to run, you don't know in which state you are anymore, or even if that's a valid state in your program. That's extremely dangerous since you can't predict your program's behaviour. The node documentation does a good job at explaining the dangers of catch-and-continue that Promises use.

Catching errors automatically has some other issues in that errors thrown asynchronously can't be caught (the stack frames when the error is thrown is not under the function that started the process anymore).

While the example you posted should crash the process, from what I understood it's not doing that, and instead it's just not completing the test case instead. That may be your test runner handling these errors for you (e.g.: with process.on('uncaughtException')). If you run it directly it crashes the process, as expected:

capturar

@ron-liu
Copy link
Author

ron-liu commented Sep 25, 2017

@robotlolita yeah, the issue is from jest, I think. I tried same test on mocha, it is fine.

I will update if I solve the jest issue.

@ron-liu
Copy link
Author

ron-liu commented Sep 26, 2017

Fixed the issue, I just need to add --env node when run jest, check this jest issue link for more details.

The issue is not related with folktale, all because of the test runner I am using.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants