From 5d59c50e13ea76fa729f0e4b8d34287ee2d6ae58 Mon Sep 17 00:00:00 2001 From: Wojciech Maj Date: Thu, 19 Nov 2020 12:09:38 +0100 Subject: [PATCH] Refactor asyncMapStrict not to use async It doesn't use async/await, but is not suitable for use with synchronous functions --- src/map_strict.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/map_strict.js b/src/map_strict.js index 87ffb7a..1625653 100644 --- a/src/map_strict.js +++ b/src/map_strict.js @@ -1,12 +1,13 @@ -export default async function asyncMapStrict(arr, fn) { - const result = []; - - for (let idx = 0; idx < arr.length; idx += 1) { - const cur = arr[idx]; - - // eslint-disable-next-line no-await-in-loop - result.push(await fn(cur, idx, arr)); - } - - return result; +export default function asyncMapStrict(arr, fn) { + return new Promise((resolve) => { + const result = []; + arr.reduce( + (promise, cur, idx) => promise + .then(() => fn(cur, idx, arr) + .then((res) => { + result.push(res); + })), + Promise.resolve(), + ).then(() => resolve(result)); + }); } -- 2.51.2