Reputation: 309
I recently ran in a, for me, strange error. I have this simple function returning a promise:
create(data) {
let _self = this;
return new Promise(function(resolve, reject) {
if(_self._validateData(data)) {
//is valid, so store in database...
} else {
reject(_self.error);
}
});
}
When the data is valid, the data is stored and there is no problem calling the resolve callback. However, when the data isn't valid, I immediately call the reject function. But when I do so I receive an error.
Is it possible that, at the time the process reaches the call of reject, the reject was not set by the calling function? Because when I run following code, it works perfectly:
create(data) {
let _self = this;
return new Promise(function(resolve, reject) {
if(_self._validateData(data)) {
//is valid, so store in database...
} else {
setTimeout(function(){reject(_self.error);}, 100);
}
});
}
I really want to avoid using this solution. Not only because it looks ugly to me, but also because I am not even sure, if I understand the error.
So any ideas what is wrong here and how can I solve this?
For everybody facing the same problem: It is not really an error but a "feature" of VS Code Debugger (using v8). See here and here for further information. The issue is called "synchron rejection". Also, I only misinterpreted it as an error. It is not. The debugger just pauses and points out, that there is an synchron rejection I moved on using bluebird promises
Upvotes: 0
Views: 62
Reputation: 664185
Is it possible that, at the time the process reaches the call of reject, the reject was not set by the calling function?
No, that's not possible. You can reject the promise at any time.
Because when I [put it in a timeout], it works perfectly
I'll guess that you did not set up a rejection handler (.catch(…)
) on your promise chain soon enough, and are receiving a "possibly unhandled promise rejection" warning because of that.
Upvotes: 1