Skip to content

Instantly share code, notes, and snippets.

@YuCJ
Created August 7, 2018 07:56
Show Gist options
  • Save YuCJ/647ecf0614ac4c653838abd32c443907 to your computer and use it in GitHub Desktop.
Save YuCJ/647ecf0614ac4c653838abd32c443907 to your computer and use it in GitHub Desktop.
function getList() {
const list = [1, 2, 0, 3, 4]
return Promise.resolve(list)
}
function task() {
return new Promise((resolve, reject) => {
getList()
.then((list) => {
let hasFailure = false
console.log('1. log out list')
list.forEach(v => {
console.log(v)
if (v === 0) {
hasFailure = true
}
})
if (hasFailure) {
reject(new Error('there is no zero'))
}
resolve('no zero')
})
.then(() => {
console.log('2. other tasks')
})
.catch(reject)
})
}
task().then((r) => { console.log(r) }).catch(e => { console.log(e) })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment