Skip to content

Instantly share code, notes, and snippets.

@jiangtao
Created November 15, 2018 09:05
Show Gist options
  • Save jiangtao/c5b7abc19c335ae0a3298d871b700507 to your computer and use it in GitHub Desktop.
Save jiangtao/c5b7abc19c335ae0a3298d871b700507 to your computer and use it in GitHub Desktop.
promise catch
const sleep = (fn, d = 500, fail = '') => new Promise((resolve, reject) => {
setTimeout(() => {
if(fail.length) {
reject(fail)
return
} else {
resolve(fn())
}
}, d)
})
const main = async function() {
console.log(await sleep(() => 1))
console.log(await sleep(() => 2, 1000, '姜鹏很帅'))
console.log(await sleep(() => 3))
}
try{
main()
}catch(e) {
console.log(e)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment