Skip to content

Instantly share code, notes, and snippets.

@export-mike
Last active June 30, 2020 22:30
Show Gist options
  • Select an option

  • Save export-mike/ae5955c6917051249a0f9b45a1ef7685 to your computer and use it in GitHub Desktop.

Select an option

Save export-mike/ae5955c6917051249a0f9b45a1ef7685 to your computer and use it in GitHub Desktop.

Revisions

  1. export-mike revised this gist Jul 18, 2017. 1 changed file with 33 additions and 26 deletions.
    59 changes: 33 additions & 26 deletions compose-async.js
    Original file line number Diff line number Diff line change
    @@ -3,49 +3,56 @@
    * Works in chrome, simply copy and paste into console.
    */

    const R = require('ramda');
    const compose =
    (...funcs) =>
    (...args) =>
    funcs.reduceRight(async (a, f) => {
    if (Array.isArray(a)) {
    return await f.apply(undefined, await a);
    }
    return await f(await a);
    }, args)
    (...funcs) =>
    (...args) =>
    funcs.reduceRight(async (a, f) => {
    if (Array.isArray(a)) {
    return await f.apply(undefined, await a);
    }
    return await f(await a);
    }, args)

    const get = (v) => {
    return Promise.resolve(`${v}s`);
    return Promise.resolve(`${v}s`);
    }

    const getAllWithError = compose(
    const getAllError = compose(
    get,
    () => Promise.reject('Error! :('),
    get,
    get
    );

    const getAll = compose(
    get,
    get,
    get,
    get
    );


    const getAllR = R.composeP(
    get,
    get,
    get,
    get
    )
    async function main() {
    try {
    const v = await getAllError('ted');
    console.log('result', v);
    } catch (e) {
    console.error(e);
    }

    try {
    const v = await getAllWithError('ted');
    console.log('result', v);
    } catch (e) {
    console.error(e);
    }

    try {
    const v = await getAll('ted');
    console.log('result', v);
    } catch (e) {
    console.error(e);
    }
    const v = await getAll('ted');
    console.log('result', v);
    } catch (e) {
    console.error(e);
    }

    console.log(await get(await get(await get('ted'))));
    console.log(await getAllR('ramda'))
    }

    main();
    main();
  2. export-mike created this gist Jul 18, 2017.
    51 changes: 51 additions & 0 deletions compose-async.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    /*
    * Requires Node 8+
    * Works in chrome, simply copy and paste into console.
    */

    const compose =
    (...funcs) =>
    (...args) =>
    funcs.reduceRight(async (a, f) => {
    if (Array.isArray(a)) {
    return await f.apply(undefined, await a);
    }
    return await f(await a);
    }, args)

    const get = (v) => {
    return Promise.resolve(`${v}s`);
    }

    const getAllWithError = compose(
    get,
    () => Promise.reject('Error! :('),
    get,
    get
    );

    const getAll = compose(
    get,
    get,
    get,
    get
    );


    async function main() {
    try {
    const v = await getAllWithError('ted');
    console.log('result', v);
    } catch (e) {
    console.error(e);
    }

    try {
    const v = await getAll('ted');
    console.log('result', v);
    } catch (e) {
    console.error(e);
    }
    }

    main();