const fn = (...args) => { for (const arg of args) { console.log(arg); } }; const [a, b] = [1, 2]; fn`${a} + ${b} = ${a + b}`;
setTimeout(() => {
console.log(1)
}, 0);
const promise = new Promise((resolve, reject) => {
console.log(2)
reject(3)
console.log(4)
})
promise
.then(() => console.log(5))
.catch(() => console.log(6))
.then(() => console.log(7))
.catch(() => console.log(8))
.then(() => console.log(9)) console.log(10)
例如你有构造了一个对象 user // { age: 10 }, 那么 user.age // 11 user.age // 12 ... 如何构造一个这样的对象,让它的属性值每次被访问时就 + 1
let executeCount = 0;
const targetFn = async nums => {
executeCount++;
return nums.map(num => 2 * num + 1);
};
const batcher = (fn) => {
// todo batch logic
}
const batchedFn = batcher(targetFn);
const main = async () => {
const [result1, result2, result3] = await Promise.all([
batchedFn([1, 2, 3]),
batchedFn([4, 5]),
batchedFn([6, 7]),
]);
console.log(result1, result2, result3)
console.log(executeCount) // 预期为 1
}
main()
batch 一个函数,让它在并发调用时只被执行一次
快排非递归实现
全部评论
(5) 回帖