Last active
October 29, 2018 10:49
-
-
Save phantomk/79f1e7a3df8507a1c609c032ce8de5ea to your computer and use it in GitHub Desktop.
This file contains hidden or 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 getRandom = () => +(Math.random() * 1000).toFixed(0); | |
const asyncTask = taskID => new Promise(resolve => { | |
let timeout = getRandom(); | |
console.log(`taskID=${taskID} start.`); | |
setTimeout(function () { | |
console.log(`taskID=${taskID} finished in time=${timeout}.`); | |
resolve(taskID) | |
}, timeout); | |
}); | |
Promise.all([asyncTask(1), asyncTask(2), asyncTask(3)]) | |
.then(resultList => { | |
console.log('results:', resultList); | |
}); | |
const asyncTaskErr = taskID => new Promise((resolve, reject) => { | |
let timeout = getRandom(); | |
console.log(`taskErrID=${taskID} start.`); | |
setTimeout(function () { | |
console.log(`taskErrID=${taskID} finished in time=${timeout}.`); | |
reject(taskID) | |
}, timeout); | |
}); | |
Promise.all([asyncTaskErr(1), asyncTaskErr(2), asyncTaskErr(3)]) | |
.then(resultList => { | |
console.log('task err results:', resultList); | |
}).catch(e => { | |
console.error('task err err', e); | |
}); | |
const asyncTaskErrWithCatch = taskID => new Promise((resolve, reject) => { | |
let timeout = getRandom(); | |
console.log(`taskErrCatchID=${taskID} start.`); | |
setTimeout(function () { | |
console.log(`taskErrCatchID=${taskID} finished in time=${timeout}.`); | |
reject(taskID) | |
}, timeout); | |
}).catch(e => { | |
console.log('taskErrCatchAtCatch', e); | |
return new Error('e'); | |
}); | |
Promise.all([asyncTaskErrWithCatch(1), asyncTaskErrWithCatch(2), asyncTaskErrWithCatch(3)]) | |
.then(resultList => { | |
console.log('task err catch results:', resultList); | |
}).catch(e => { | |
console.error('task err catch err', e); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment