I want to reject a promise which I do not built. That is, examples I've read describe something like it:
const sample = new Promise((resolve, reject) => {
setTimeout(() => {
reject('fail promise');
}, 1000);
});
That reject the sample
after 1s. In my case the promise I want to reject is coming as an external api call then I can't to reject in that way.
Another approaches I've read shows how the promise can be wrapped with other that uses a setTimeout to reject the new promise. Something like this:
const timeout = new Promise(function(resolve, reject) {
setTimeout(resolve, 1000, 'one');
});
const sample = new Promise(function(resolve, reject) {
setTimeout(resolve, 5000, 'two');
});
return Promise.race([sample, timeout]);
That force to 'reject' sample
after 1s returning the other promise. That could be used to set a timeout but in fact it does not reject sample
, only returns another promise and the original continues running until it rejects or resolve itself.
I can not find how can I reject correctly a promise without a library