question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possibility for Disposer chaining

See original GitHub issue

(This issue tracker is only for bug reports or feature requests, if this is neither, please choose appropriate channel from http://bluebirdjs.com/docs/support.html)

Please answer the questions the best you can:

  1. What version of bluebird is the issue happening on? 3.5

  2. What platform and version? (For example Node.js 0.12 or Google Chrome 32) irrelevant

  3. Did this issue happen with earlier version of bluebird? probably

I want to achieve some kind of disposer chaining. I have one resource for its construction another resource is needed. So my resource must be disposed before source resource is disposed.

function getClient() {
    return pg.connect(connString).disposer(function(client) {
        console.log('CLIENT RELEASE');
        client.release();
    });
}

// getter for depended resource
function getLOM() {
    return Promise.using(getClient(), function(client) {
        return Promise.resolve(new LargeObjectManager(client)).disposer(function(lom) {
            lom.close();
        });
    });
}

// desired usage
Promise.using(getLOM(), function(lom) {
    // do something
});

The problem is that client is disposed before LargeObjectManager (dependent object). I am aware of that I can rewrite it using callback but this is not exactly what I want.

Fallback solution

function useLom(fn) {
    return Promise.using(getClient(), function(client) {
        return Promise.using(Promise.resolve(new LargeObjectManager(client)).disposer(function(lom) {
            lom.close();
        }), fn);
    });
}

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
spioncommented, May 14, 2017

I think this idea makes sense, but the API needs to be thought out. We can reopen if there is a good and not overly complex API proposal for it? Perhaps something like this?

function getLOM() {
    return getClient().derive(client => new LargeObjectManager(client), lom => lom.close())
}

Although I suppose it would also need to support deriving from multiple other disposers, so maybe a static method taking one or more arguments like Promise.using would be better.

0reactions
kjvalencikcommented, Aug 17, 2017

Here’s what I came up using only public methods:

function derive(...args) {
	const disposer = args.pop();
	const f = args.pop();

	if (!args.length) {
		return Promise.reject(
			new TypeError('you must pass at least 2 arguments to derive')
		);
	}

	if (typeof f !== 'function') {
		return Promise.reject(
			new TypeError('Derivation method must be a function')
		);
	}

	if (typeof disposer !== 'function') {
		return Promise.reject(
			new TypeError('Disposer must be a function')
		);
	}

	let innerResolve = null;
	let p = null;

	return new Promise((outerResolve, outerReject) => (
		p = Promise.using(...args, (...resources) => (
			Promise
				.try(() => f(...resources))
				.then(derived => (outerResolve(derived), new Promise(resolve => (
					innerResolve = resolve
				))))
				.catch(err => outerReject(err))
		))
	)).disposer(() => (
		Promise.try(disposer).finally(() => (innerResolve(), p))
	));
}

Usage:

function getDerivedObject() {
    return derive(getDisposableThing(), thing => deriveFrom(thing), derived => derived.destroy());
}

Bluebird.using(getDerivedObject(), derived => derived);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Optimization Model for Integrated Municipal Solid Waste ...
The proposed network is considered a dynamic (multiperiod) supply chain. To better understand the research problem, Figure 1 illustrates the ...
Read more >
Dr. Disposal Garbage Disposal Installation Tool Steel
This thing really works! Drop the end piece in the disposal, pull up thru sink and attach bar in a chain link to...
Read more >
Recycling a kitchen garbage disposer motor | Practical Machinist
Has anyone figured out a shop use for a kitchen garbage disposer motor. I just replaced mine inwhich the aluminum housing had corroded ......
Read more >
Solid waste issue: Sources, composition, disposal, recycling ...
Often, such organic contents are excluded from this value added chain. It ends up on streets or accumulates on dumpsites, despite its energy...
Read more >
3/4-Horsepower Continuous Feed Food Waste Disposer
KitchenAid 3/4-Horsepower Continuous Feed Food Waste Disposer - Red (KCDS075T). Shop now.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found