Another example:
const expensiveOperation = async (value) => {
// return Promise.resolve(value)
// console.log('value:', value)
await sleep(1000)
console.log('expensiveOperation: value:', value, 'finished')
return value
}
var expensiveOperationDebounce = debounce(expensiveOperation, 100);
// for (let num of [1, 2]) {
// expensiveOperationDebounce(num).then(value => {
// console.log(value)
// })
// }
(async () => { await sleep(0 ); console.log(await expensiveOperationDebounce(1)) })();
(async () => { await sleep(200 ); console.log(await expensiveOperationDebounce(2)) })();
(async () => { await sleep(1300); console.log(await expensiveOperationDebounce(3)) })();
// setTimeout(async () => {
// console.log(await expensiveOperationDebounce(3))
// }, 1300)
Outputs: 1, 2, 3
Why, if I change it to:
(async () => { await sleep(0 ); console.log(await expensiveOperationDebounce(1)) })();
(async () => { await sleep(200 ); console.log(await expensiveOperationDebounce(2)) })();
(async () => { await sleep(1100); console.log(await expensiveOperationDebounce(3)) })();
Does it only output 2, 3?