Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save AwokeKnowing/a8c6ec3165946fd2d49de0c3f4b434c3 to your computer and use it in GitHub Desktop.

Select an option

Save AwokeKnowing/a8c6ec3165946fd2d49de0c3f4b434c3 to your computer and use it in GitHub Desktop.

Revisions

  1. @DavidWells DavidWells revised this gist Feb 22, 2020. No changes.
  2. @DavidWells DavidWells revised this gist Feb 22, 2020. No changes.
  3. @DavidWells DavidWells revised this gist Feb 22, 2020. No changes.
  4. @DavidWells DavidWells renamed this gist Apr 20, 2019. 1 changed file with 18 additions and 15 deletions.
    33 changes: 18 additions & 15 deletions nice-try-catch.js → no-try-catch-async-await.js
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,24 @@
    /* Wrap promises in nicer try/catch */
    /* 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])
    }

    /* Normal promise thingy */
    /* 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)
    }

    /* Normal promise thingy */
    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) => {
    // Run the thing
    const params = {lol: 'true'}
    usageExample(params).then((result) => {
    console.log('result', result)
    }).catch((err) => {
    console.log('error', err)
  5. @DavidWells DavidWells revised this gist Apr 20, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions nice-try-catch.js
    Original 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)
    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
    // handle or throw err
    throw new Error(err)
    }
    // Do stuff with data
  6. @DavidWells DavidWells revised this gist Apr 20, 2019. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions nice-try-catch.js
    Original 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) {
    return err
    // handle or throw err
    throw new Error(err)
    }
    console.log('data', data)
    // Do stuff with data
    return data
    }

    usageExample({lol: 'true'}).then((result) => {
    console.log(result)
    var x = usageExample({lol: 'true'}).then((result) => {
    console.log('result', result)
    }).catch((err) => {
    console.log('error', err)
    })
  7. @DavidWells DavidWells renamed this gist Apr 20, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. @DavidWells DavidWells created this gist Apr 20, 2019.
    35 changes: 35 additions & 0 deletions gistfile1.txt
    Original 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)
    })