「resolve」や「reject」を使って値を送った際に、「async/await」の記法で受け取る例です。
const timer = (time, type) => { return new Promise((resolve, reject) => { setTimeout(() => { if (type === 'resolve') { resolve(`${time} 解決`); } if (type === 'reject') { reject(`${time} 拒否`); } }, time); }); }; (async function() { try { const res1 = await timer(500, 'resolve'); console.log(res1); const res2 = await timer(600, 'reject'); console.log(res2); const res3 = await timer(700, 'resolve'); console.log(res3); } catch(e) { console.log(e); } console.log('終了'); })();
500 解決 600 拒否 終了