plugAPI icon indicating copy to clipboard operation
plugAPI copied to clipboard

Use ES6 Promise in place of callbacks

Open mortenn opened this issue 7 years ago • 5 comments

I think this would be a beneficial change long term - 6.0?

mortenn avatar Oct 16 '17 20:10 mortenn

if people want to use Promise, node v8's promisfy or bluebird's promisfy function is a viable option. 90% of the callbacks plugAPI provides is in the correct form of callback(err, data) => {}; (There are a few that only callback(data) )

I see no benefit of having to rewrite the entire lib to use promises and making people change their existing code to have to use promises.

thedark1337 avatar Oct 16 '17 20:10 thedark1337

Do either v8's or bluebirds promisify work on PAPI yet? last time I checked they did not

Dazzuh avatar Oct 16 '17 20:10 Dazzuh

In my opinion, using a standard feature of the platform is a benefit in and of itself.

mortenn avatar Oct 16 '17 21:10 mortenn

As a kind of compromise, maybe methods that take a callback could return a promise if called without a callback argument?

mortenn avatar Oct 16 '17 21:10 mortenn

As an interim solution, I wrote this function:

function toPromise(call)
{
	return new Promise(
		function(resolve, reject)
		{
			call(
				function(err, data)
				{
					if(err)
						return reject(err);
					resolve(data);
				}
			);
		}
	);
}

Or, if you want to be slightly more evil:

function toPromise(call)
{
	return new Promise(
		(resolve, reject) => call((err, data) => err ? reject(err) : resolve(data))
	);
}

;)

mortenn avatar Oct 17 '17 18:10 mortenn