Reputation: 601
var promise = new Promise(function(resolve, reject) {
try {
throw new Error('test');
} catch(e) {
reject(e);
}
});
promise.catch(function(error) {
console.log(error);
});
we can use "try-catch" to throw error
// mothod 2
var promise = new Promise(function(resolve, reject) {
reject(new Error('test'));
});
promise.catch(function(error) {
console.log(error);
});
we also can use "reject" to throw error.
what's difference between them?
Upvotes: 0
Views: 1532
Reputation: 54
Upvotes: 2
Reputation: 101662
There is no effective difference. In both cases, you're calling reject()
with an error.
The following are also equivalent to what you have there:
var promise = new Promise(function(resolve, reject) {
throw new Error('test');
});
var promise = Promise.reject(new Error('test'));
var promise = Promise.resolve().then(function () { throw new Error('test'); });
Upvotes: 0