このページの以降のコードで使う共通コードです。
const timer = (time, type) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (type === 'resolve') {
resolve(`${time} 解決`);
}
if (type === 'reject') {
reject(`${time} 拒否`);
}
}, time);
});
};
「resolve」を実行した場合の「finally」の例です。
timer(500, 'resolve') .then(res => { console.log('then:', res); }) .catch(res => { console.log('catch:', res); }) .finally(() => { console.log('finally: 終了'); });
then: 500 解決
finally: 終了
「reject」を実行した場合の「finally」の例です。
timer(500, 'reject') .then(res => { console.log('then:', res); }) .catch(res => { console.log('catch:', res); }) .finally(() => { console.log('finally: 終了'); });
catch: 500 拒否
finally: 終了