1. const asyncSequentializer = (() => {
    2. const toPromise = (x) => {
    3. if(x instanceof Promise) { // if promise just return it
    4. return x;
    5. }
    6. if(typeof x === 'function') {
    7. // if function is not async this will turn its result into a promise
    8. // if it is async this will await for the result
    9. return (async () => await x())();
    10. }
    11. return Promise.resolve(x)
    12. }
    13. return (list) => {
    14. const results = [];
    15. return list
    16. .reduce((lastPromise, currentPromise) => {
    17. return lastPromise.then(res => {
    18. results.push(res); // collect the results
    19. return toPromise(currentPromise);
    20. });
    21. }, toPromise(list.shift()))
    22. // collect the final result and return the array of results as resolved promise
    23. .then(res => Promise.resolve([...results, res]));
    24. }
    25. })()