You can't structure it the way you did because the while() loop will run forever and never allow even the first .then() handler to run (due to timing and event loop reasons). So, you can't just normally break your while loop from within the .then() handler.
If you can use async/await, then this is more straightforward:
while (relevantRefundRequests.length >= waitList[0].quantity) {
let data = await stripe.paymentIntents.create({ ... });
if (some condition) {
break;
}
}
Keep in mind that the containing function here will have to be tagged async and will return long before this loop is done so you have to handle that appropriately.
If you can't use async/await, then the code needs to be restructured to use control structures other than a while loop and it would be helpful to see the real code (not just pseudo code) to make a more concrete suggestion for how best to do that. A general idea is like this:
function runAgain() {
if (relevantRefundRequests.length >= waitList[0].quantity) {
return stripe.paymentIntents.create({ ... }).then(data => {
if (some condition to break the cycle) {
return finalResult;
} else {
// run the cycle again
return runAgain();
}
});
} else {
return Promise.resolve(finalResult);
}
}
runAgain().then(finalResult => {
console.log(finalResult);
}).catch(err => {
console.log(err);
});