"use strict";

/*
* Creates a version of λ that cannot be executed concurrently.
*
* Returns the Promise returned by λ.
*/
module.exports = λ => {
    let locked  = false;
    let promise = null;

    return () => {
        if (locked) {
            return promise;
        }

        locked = true;

        promise = λ().then(
            results => {
                locked = false;

                return results;
            },

            error => {
                locked = false;

                return Promise.reject(error);
            }
        );

        return promise;
    };
};

"use strict";

/*
* Creates a version of λ that cannot be executed concurrently.
*
* Returns the Promise returned by λ.
*/
module.exports = λ => {
    let locked  = false;
    let promise = null;

    return async () => {
        if (locked) {
            return promise;
        }

        locked = true;

        try {
            const results = await λ();

            locked = false;

            return results;
        } catch (error) {
            locked = false;

            return Promise.reject(error);
        }
    };
};

Allow noConcurrentCalls to work with non-async λs.
Allow noConcurrentCalls to work with non-async λs.
