created by Aha00aAha00a at 2021-05-05
last modified by Aha00aAha00a at 2022-12-15
revision: 4

JavaScript Promise

1. with Array.map() and Array.reduce()

(async () => {
    const delayed = v => new Promise(resolve => setTimeout(() => {
        console.log(`delayed - ${v}`);
        return resolve(v);
    }, Math.random() * 1000));

    const test = [1, 2, 3, 4, 5];

    const testMap = await Promise.all(test.map(async v => await delayed(v)));
    console.log(testMap);

    const testReduce = await test.reduce(async (a, v) => [...(await a), (await delayed(v))], []);
    console.log(testReduce);
})();
delayed - 2
delayed - 4
delayed - 5
delayed - 3
delayed - 1
(5) [1, 2, 3, 4, 5]
delayed - 1
delayed - 2
delayed - 3
delayed - 4
delayed - 5
(5) [1, 2, 3, 4, 5]

Array.map()을 사용하면 병렬실행을 할 수 있고, Array.reduce()를 이용하여 순차실행을 할 수 있다.

--2021-05-05

혼자 이거 저거 테스트 해 보고 결론 내렸는데, 진작 검색부터 해 볼걸.. 많은 글들이 있는데, 아래 글이 훌륭하다.

3. See Also

3.2. Similar Pages

Similar pages by cosine similarity. Words after page name are term frequency.

3.3. Adjacent Pages

≤ 32
all
1.0x
1.0x
80
-120
ON
0/0
0/0
0.00
0
0 / 0
1.00x