Sha256: 4b1e66df907b009a7d74cf8ec5ed3be34e32cf5417570793605e6d7301af1cd9

Contents?: true

Size: 798 Bytes

Versions: 7

Compression:

Stored size: 798 Bytes

Contents

'use strict'

module.exports.disposer = disposer

function disposer (creatorFn, disposerFn, fn) {
  const runDisposer = (resource, result, shouldThrow = false) => {
    return disposerFn(resource)
      .then(
        // disposer resolved, do something with original fn's promise
        () => {
          if (shouldThrow)
            throw result

          return result
        },
        // Disposer fn failed, crash process
        (err) => {
          throw err
          // Or process.exit?
        })
  }

  return creatorFn
    .then((resource) => {
      // fn(resource) can throw, so wrap in a promise here
      return Promise.resolve().then(() => fn(resource))
        .then((result) => runDisposer(resource, result))
        .catch((err) => runDisposer(resource, err, true))
    })
}

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
disco_app-0.16.1 test/dummy/node_modules/cacache/lib/util/disposer.js
disco_app-0.15.2 test/dummy/node_modules/cacache/lib/util/disposer.js
disco_app-0.18.4 test/dummy/node_modules/cacache/lib/util/disposer.js
disco_app-0.18.1 test/dummy/node_modules/cacache/lib/util/disposer.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/cacache/lib/util/disposer.js
disco_app-0.14.0 test/dummy/node_modules/cacache/lib/util/disposer.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/cacache/lib/util/disposer.js