Last change
on this file since 59329aa was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
798 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | 'use strict'
|
---|
| 2 |
|
---|
| 3 | module.exports.disposer = disposer
|
---|
| 4 |
|
---|
| 5 | function disposer (creatorFn, disposerFn, fn) {
|
---|
| 6 | const runDisposer = (resource, result, shouldThrow = false) => {
|
---|
| 7 | return disposerFn(resource)
|
---|
| 8 | .then(
|
---|
| 9 | // disposer resolved, do something with original fn's promise
|
---|
| 10 | () => {
|
---|
| 11 | if (shouldThrow)
|
---|
| 12 | throw result
|
---|
| 13 |
|
---|
| 14 | return result
|
---|
| 15 | },
|
---|
| 16 | // Disposer fn failed, crash process
|
---|
| 17 | (err) => {
|
---|
| 18 | throw err
|
---|
| 19 | // Or process.exit?
|
---|
| 20 | })
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | return creatorFn
|
---|
| 24 | .then((resource) => {
|
---|
| 25 | // fn(resource) can throw, so wrap in a promise here
|
---|
| 26 | return Promise.resolve().then(() => fn(resource))
|
---|
| 27 | .then((result) => runDisposer(resource, result))
|
---|
| 28 | .catch((err) => runDisposer(resource, err, true))
|
---|
| 29 | })
|
---|
| 30 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.