Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Created December 30, 2020 11:03
Show Gist options
  • Select an option

  • Save WebReflection/e91dab813698100a4e0738858d582093 to your computer and use it in GitHub Desktop.

Select an option

Save WebReflection/e91dab813698100a4e0738858d582093 to your computer and use it in GitHub Desktop.

Revisions

  1. WebReflection created this gist Dec 30, 2020.
    18 changes: 18 additions & 0 deletions async-tag.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    const {isArray} = Array;

    const sync = async values => {
    for (let {length} = values, i = 0; i < length; i++) {
    const value = await values[i];
    values[i] = isArray(value) ? await sync(value) : value;
    }
    return values;
    };

    const asyncTag = tag => async (template, ...values) =>
    tag(template, ...(await sync(values)));

    // example
    const asyncRaw = asyncTag(String.raw);

    asyncRaw`a${Promise.resolve('b')}c`
    .then(console.log); // "abc"