Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Last active April 14, 2022 15:32
Show Gist options
  • Save WebReflection/e77838e233d56307d8b66a1332a41fd5 to your computer and use it in GitHub Desktop.
Save WebReflection/e77838e233d56307d8b66a1332a41fd5 to your computer and use it in GitHub Desktop.

Revisions

  1. WebReflection revised this gist Nov 5, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion async-await.md
    Original file line number Diff line number Diff line change
    @@ -53,5 +53,5 @@ const fn = any => new Promise(resolve => {
    });

    // and that's *all* we need, anything else
    // is unnecessary overhead no software need
    // is unnecessary overhead.
    ```
  2. WebReflection revised this gist Nov 5, 2020. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions async-await.md
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ const fn = (...args) => new Promise(resolve => {
    const original = (...args) => {/* stuff */};
    // and resolves it
    resolve(original(...args));
    // if an error is throws and no try/catch
    // if an error is thrown and no try/catch
    // is around, it breaks outer execution
    });

    @@ -25,7 +25,7 @@ Promise.resolve(fn()).then(result => {
    // the program pauses until result is known
    // and it keeps executing with val as result
    const val = result;
    // if fn() throw an error and no try/catch
    // if fn() throws an error and no try/catch
    // is around, it breaks outer execution
    });

    @@ -36,9 +36,11 @@ const fn = async any => await any;
    // is the equivalent of this
    const fn = any => new Promise(resolve => {
    const original = any => Promise.resolve(any);
    // ^^^^^^^^^^^^^^^
    original.then(resolve);
    // can you see that there are 2 resolves
    // while one would be enough?
    // ^^^^^^^
    // can you see that there are 2 resolves for
    // the exact same value?
    });

    // that means that this:
  3. WebReflection revised this gist Nov 5, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions async-await.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # async / return await explained

    This is yet another explanation of why `async` and `return await` is pointless, coming from [this post](https://webreflection.medium.com/a-better-async-await-approach-2d130de5f39a).

    ```js
  4. WebReflection created this gist Nov 5, 2020.
    53 changes: 53 additions & 0 deletions async-await.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    This is yet another explanation of why `async` and `return await` is pointless, coming from [this post](https://webreflection.medium.com/a-better-async-await-approach-2d130de5f39a).

    ```js

    // async means that this:
    const fn = async (...args) => {/* stuff */};

    // is basically the equivalent of this:
    const fn = (...args) => new Promise(resolve => {
    // async wraps the original callback as Promise
    const original = (...args) => {/* stuff */};
    // and resolves it
    resolve(original(...args));
    // if an error is throws and no try/catch
    // is around, it breaks outer execution
    });

    // await means that this:
    const val = await fn();

    // is basically the equivalent of this:
    Promise.resolve(fn()).then(result => {
    // the program pauses until result is known
    // and it keeps executing with val as result
    const val = result;
    // if fn() throw an error and no try/catch
    // is around, it breaks outer execution
    });


    // so that this:
    const fn = async any => await any;

    // is the equivalent of this
    const fn = any => new Promise(resolve => {
    const original = any => Promise.resolve(any);
    original.then(resolve);
    // can you see that there are 2 resolves
    // while one would be enough?
    });

    // that means that this:
    const fn = async any => any;

    // becomes this:
    const fn = any => new Promise(resolve => {
    const original = any => any;
    resolve(original(any));
    });

    // and that's *all* we need, anything else
    // is unnecessary overhead no software need
    ```