// 実行する関数
const sampleFunc = (value) => {
// asyncの効果は各functionブロックで切れるので逐一指定が必要
return new Promise(resolve => {
// 2秒待ってから計算結果をresolveする
setTimeout(() => {
console.log('Calculating...');
resolve(value * 2);
}, value*1000);
})
}
// 対象の反復オブジェクト
const targetArr = [6, 4, 2].map(sampleFunc);
// for await...of文は必ずasyncの中で
(async () => {
for await (num of targetArr) {
// 関数の実行結果を格納して表示
console.log(num);
}
})();
Ly8g5a6f6KGM44GZ44KL6Zai5pWwCmNvbnN0IHNhbXBsZUZ1bmMgPSAodmFsdWUpID0+IHsKLy8gYXN5bmPjga7lirnmnpzjga/lkIRmdW5jdGlvbuODluODreODg+OCr+OBp+WIh+OCjOOCi+OBruOBp+mAkOS4gOaMh+WumuOBjOW/heimgQogICAgcmV0dXJuIG5ldyBQcm9taXNlKHJlc29sdmUgPT4geyAKICAgICAgICAvLyAy56eS5b6F44Gj44Gm44GL44KJ6KiI566X57WQ5p6c44KScmVzb2x2ZeOBmeOCiwogICAgICAgIHNldFRpbWVvdXQoKCkgPT4gewogICAgICAgICAgICBjb25zb2xlLmxvZygnQ2FsY3VsYXRpbmcuLi4nKTsKICAgICAgICAgICAgcmVzb2x2ZSh2YWx1ZSAqIDIpOwogICAgICAgIH0sIHZhbHVlKjEwMDApOwogICAgfSkKfQoKLy8g5a++6LGh44Gu5Y+N5b6p44Kq44OW44K444Kn44Kv44OICmNvbnN0IHRhcmdldEFyciA9IFs2LCA0LCAyXS5tYXAoc2FtcGxlRnVuYyk7CgovLyBmb3IgYXdhaXQuLi5vZuaWh+OBr+W/heOBmmFzeW5j44Gu5Lit44GnCihhc3luYyAoKSA9PiB7CiAgZm9yIGF3YWl0IChudW0gb2YgdGFyZ2V0QXJyKSB7CiAgICAvLyDplqLmlbDjga7lrp/ooYzntZDmnpzjgpLmoLzntI3jgZfjgabooajnpLoKICAgIGNvbnNvbGUubG9nKG51bSk7CiAgfQp9KSgpOw==