I have the following chained promise. At each step, I need to evaluate if the returned value is not null. I can add an if else condition at each step, but I was wondering if there is a more concise way of doing this. Also, how can I break out of the chain if the value is null at any step?
axios.post('/api/login', accounts) .then((response) => { this.nonce = response.data return this.nonce }).then((nonce) => { let signature = this.signing(nonce) return signature }).then((signature) => { this.verif(signature) }) .catch((errors) => { ... })
Advertisement
Answer
You break out of the promise chain by throwing an error:
axios.post('/api/login', accounts) .then((response) => { this.nonce = response.data return this.nonce }).then((nonce) => { if (!nonce) throw ("no nonce") let signature = this.signing(nonce) return signature }).then((signature) => { if (!signature) throw ("no signature") this.verif(signature) }) .catch((errors) => { ... })