Last active
April 14, 2022 15:32
-
-
Save WebReflection/e77838e233d56307d8b66a1332a41fd5 to your computer and use it in GitHub Desktop.
Revisions
-
WebReflection revised this gist
Nov 5, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. ``` -
WebReflection revised this gist
Nov 5, 2020 . 1 changed file with 6 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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() 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 for // the exact same value? }); // that means that this: -
WebReflection revised this gist
Nov 5, 2020 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
WebReflection created this gist
Nov 5, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ```