I have a chain of promises, where certain things happen on resolve and different things happen on reject, however sometimes I want to skip all the following then
statements.
My code looks like this
await req.reduce((promise, audit) => {
let globalData;
return promise.then(_ => this.add(audit)
.then((data)=> {
globalData = data; console.log('1');
return dropbox_functions.createFolder(data.ui, data)
},
(error)=> {
failed.push({audit: audit, error: 'There was an error adding this case to the database'});
console.log('1.1'); i = i + 1; socket.emit('bulkAddUpdate', i/arrLen);
throw new Error('There was an error adding this case to the database');
})
.then((data)=>{
console.log('2');
return dropbox_functions.checkScannerFolderExists(audit.scanner_ui)
},
(error)=>{
console.log('2.1');issues.push({audit: globalData, error: 'There was an error creating the case folder in dropbox'});
i = i + 1;socket.emit('bulkAddUpdate', i/arrLen);
throw new Error('There was an error creating the case folder in dropbox');
})
.then((data)=>{
console.log('3');
return dropbox_functions.moveFolder(audit.scanner_ui, globalData.ui)},
(error)=>{
console.log('3.1');issues.push({audit: globalData, error: 'No data folder was found so an empty one was created'});
return dropbox_functions.createDataFolder(globalData.ui)
})
.then(()=> {
console.log('4');
success.push({audit:globalData});
i = i + 1;socket.emit('bulkAddUpdate', i/arrLen);},
(error)=> {
issues.push({audit: globalData, error: 'Scanner folder found but items not moved'});console.log('4.1');
})
.catch(function(error){
console.log(error)
})
);
}, Promise.resolve()).catch(error => {console.log(error)});
In the first then if the code goes into the reject case, I want to skip all the remaining thens. However it doesn't and therefore all the subsequent then happen and all fail. The only reject that should move on is in the 3rd then when I do want to return a promise.