1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
'use strict'; const p = console.log; const createPromise = ({ ms, callback, errMsg = null }) => { return new Promise((resolve, reject) => { if (errMsg) { reject(errMsg); } setTimeout(() => { const result = callback(); resolve(result); }, ms); }); }; const argsForCreatePromise = (ms, errMsg = null) => { const callback = () => `${ms}ミリ秒待ちました`; return { ms, callback, errMsg }; }; const onlyReturn = (a) => a; const asyncMain = async () => { const errMsg = 'エラーです'; p(await createPromise(argsForCreatePromise(37)).catch(onlyReturn)); p(await createPromise(argsForCreatePromise(51)).catch(onlyReturn)); p(await createPromise(argsForCreatePromise(28, errMsg)).catch(onlyReturn)); p(await createPromise(argsForCreatePromise(12)).catch(onlyReturn)); }; asyncMain(); |
1 2 3 4 |
37ミリ秒待ちました 51ミリ秒待ちました エラーです 12ミリ秒待ちました |