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 33 34 35 36 |
'use strict'; const createTimeoutPromise = (ms, callback, errMsg) => { return new Promise((resolve, reject) => { if (errMsg) { reject(errMsg); }; setTimeout(() => { resolve(callback()); }, ms); }); }; const getLastNameBy50ms = (errMsg) => { return createTimeoutPromise(50, () => "田中", errMsg); }; const getFirstNameBy30ms = (errMsg) => { return createTimeoutPromise(30, () => "太郎", errMsg); }; const getAgeBy15ms = (errMsg) => { return createTimeoutPromise(15, () => 32, errMsg); }; const main = async() => { const lastName = await getLastNameBy50ms().catch(a => a); const firstName = await getFirstNameBy30ms("名前が取得できないエラー").catch(a => a); const age = await getAgeBy15ms().catch(a => a); console.log("lastName : " + lastName); console.log("firstName : " + firstName); console.log("age : " + age); }; main(); |
1 2 3 |
lastName : 田中 firstName : 名前が取得できないエラー age : 32 |