処理完了順
function func() {
const data = [1, 4, 3, 2,];
for (let i = 0; i < data.length; i += 1) {
new Promise(resolve => {
setTimeout(() => {
resolve(data[i]);
}, 1000 * data[i]);
}).then(result => console.info(result));
}
console.info('for-end');
return 'DONE!';
}
func();
実行順
async function async_func() {
const data = [1, 4, 3, 2,];
for (let i = 0; i < data.length; i += 1) {
const result = await new Promise(resolve => {
setTimeout(() => {
resolve(data[i]);
}, 1000 * data[i]);
});
console.info(result);
}
console.info('for-end');
return 'DONE!';
}
async_func().then((d) => {
console.info(d);
});