-
-
Save AwokeKnowing/a8c6ec3165946fd2d49de0c3f4b434c3 to your computer and use it in GitHub Desktop.
Revisions
-
DavidWells revised this gist
Feb 22, 2020 . No changes.There are no files selected for viewing
-
DavidWells revised this gist
Feb 22, 2020 . No changes.There are no files selected for viewing
-
DavidWells revised this gist
Feb 22, 2020 . No changes.There are no files selected for viewing
-
DavidWells renamed this gist
Apr 20, 2019 . 1 changed file with 18 additions and 15 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,12 +1,24 @@ /* Helper buddy for removing async/await try/catch litter 🗑 */ function O_o(promise) { return promise.then(data => { if (data instanceof Error) return [data] return [null, data] }).catch(err => [err]) } /* Look ma, no try/catch */ async function usageExample(params) { const [ err, data ] = await O_o(myPromise(params)) if (err) { // handle or throw err throw new Error(err) } // Do stuff with data return data } /* Normal promise */ function myPromise(params) { return new Promise((resolve, reject) => { callbacker(params, (error, data) => { @@ -16,23 +28,14 @@ function myPromise(params) { }) } /* Normal callback */ function callbacker(params, cb) { return cb(null, params) } // Run the thing const params = {lol: 'true'} usageExample(params).then((result) => { console.log('result', result) }).catch((err) => { console.log('error', err) -
DavidWells revised this gist
Apr 20, 2019 . 1 changed file with 3 additions and 3 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 @@ -10,8 +10,8 @@ function O_o(promise) { function myPromise(params) { return new Promise((resolve, reject) => { callbacker(params, (error, data) => { if (error) return reject(error) return resolve(data) }) }) } @@ -25,7 +25,7 @@ async function usageExample(params) { const [ err, data ] = await O_o(myPromise(params)) if (err) { // handle or throw err throw new Error(err) } // Do stuff with data -
DavidWells revised this gist
Apr 20, 2019 . 1 changed file with 8 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 @@ -25,11 +25,15 @@ async function usageExample(params) { const [ err, data ] = await O_o(myPromise(params)) if (err) { // handle or throw err throw new Error(err) } // Do stuff with data return data } var x = usageExample({lol: 'true'}).then((result) => { console.log('result', result) }).catch((err) => { console.log('error', err) }) -
DavidWells renamed this gist
Apr 20, 2019 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
DavidWells created this gist
Apr 20, 2019 .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,35 @@ /* Wrap promises in nicer try/catch */ function O_o(promise) { return promise.then(data => { if (data instanceof Error) return [data] return [null, data] }).catch(err => [err]) } /* Normal promise thingy */ function myPromise(params) { return new Promise((resolve, reject) => { callbacker(params, (error, data) => { if (error) return reject(error) return resolve(data) }) }) } function callbacker(params, cb) { return cb(null, params) } /* Normal promise thingy */ async function usageExample(params) { const [ err, data ] = await O_o(myPromise(params)) if (err) { return err } console.log('data', data) } usageExample({lol: 'true'}).then((result) => { console.log(result) })