How to cancel a promise in javascript?

var promise1 = new Promise(function(resolve, reject) { // ajax api call }); var promise2 = new Promise(function(resolve, reject) { // ajax api call }); 

I want to be able to do something like -

 if(a < b) { promise1.cancel(); } 
+5
source share
1 answer

You cannot cancel a promise, but you can associate your promises with then and reject at any point.

 new Promise((resolve, reject) => { // step 1 }) .then((result) => { if (!result) { // Reject the promise chain throw 'cancel'; } else { return ... // step 2 } }) .catch(err => { // cancelled }); 
+2
source

Source: https://habr.com/ru/post/1262559/


All Articles