-
Notifications
You must be signed in to change notification settings - Fork 52
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
error throw inside promise.then or promise.catch function, can not get the right context #124
Comments
Any update on this? |
Use Promise.reject(error) instead of throw, or clear expired context by using setInterval as a workaround solution.
|
I think this is by design. Any call back that runs as a result of a rejected promise is conceptually a continuation from the point the error was thrown. If you want to stop the propagation of contexts into an error handler, you'll need to handle that explicitly |
@hayes @carlisliu thanks for the response I really appreciate it. I think I did a bad job describing the issue correctly. Let's have a look at the following code:
I would be expecting the following output in the console:
However what I get back is following:
@carlisliu going with your suggestion, if I do use Regarding the setTimeout to clear the context does not really help much in my case since I am using Please let me know your thoughts, I do not want to follow anti patters and refactor a huge code base with |
I can try this after work, but I have a theory. I can test it out when I get off work tonight, but the 2 cases I am currious about are removing the catch and/or replacing it with a then (and not throwing) to confirm it's related to the throwing of an error. The other is putting the ns.run into a timeout of 0. |
I have not had time to understand why, but this is the change where this behavior started: 1b94097 |
This happens because wrapCallback was designed to wrap a top level callback which does not trap errors (it expects a throw to fall through to @matthewloring I think we will need to either run the error or after handlers in the case of an error, but I'm not sure which is correct, do you have more insight into the use case behind that change? |
Hi all ! Any update on this ? |
If I understand the commit correctly, I think it can be done in other ways external to this library. In any case, that commit basically completely broke the library. I would therefore probably simply revert the change. For example: const cls = require('continuation-local-storage')
const ns = cls.createNamespace('test')
ns.run(() => {
let promise
ns.set('foo', 'bar')
ns.run(() => {
ns.set('foo', 'baz')
promise = new Promise((resolve, reject) => {
resolve()
})
})
promise
.then(() => {
console.log('Value in resolution context:', ns.get('foo'))
})
bind(promise)
.then(() => {
console.log('Value in then context:', ns.get('foo'))
})
})
function bind (promise) {
const deferred = defer()
return promise
.then(function () {
deferred.resolve.apply(deferred, arguments)
return deferred.promise
})
.catch(function () {
deferred.reject.apply(deferred, arguments)
return deferred.promise
})
}
function defer () {
const deferred = {}
deferred.promise = new Promise((resolve, reject) => {
deferred.resolve = ns.bind(resolve)
deferred.reject = ns.bind(reject)
})
return deferred
} @hayes Thoughts? |
Namespace remains active if error is thrown in catch of Promise
any idea why?
The text was updated successfully, but these errors were encountered: