Sequentially Resolve Promises Works, Giải quyết tuần tự các lời hứa hoạt động (ok)
https://css-tricks.com/why-using-reduce-to-sequentially-resolve-promises-works/

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.7/dayjs.min.js"></script>
</head>
<body>
<script>
function methodThatReturnsAPromise(nextID) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(`Resolve! ${dayjs().format('hh:mm:ss')}`);
resolve();
}, 1000);
});
}
[1, 2, 3].reduce((accumulatorPromise, nextID) => {
console.log(`Loop! ${dayjs().format('hh:mm:ss')}`);
return accumulatorPromise.then(() => {
return methodThatReturnsAPromise(nextID);
});
}, Promise.resolve());
</script>
</body>
</html>Why Using reduce() to Sequentially Resolve Promises Works
Why does this even work?
Why won’t any other Array methods work?
I hope this helps!
PreviousJavascript Array chunkArrayInGroups(arr, size)NextUsing Async / Await with the Array Reduce Method API (ok)
Last updated