Created
November 15, 2018 09:05
-
-
Save jiangtao/c5b7abc19c335ae0a3298d871b700507 to your computer and use it in GitHub Desktop.
promise catch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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