plugAPI
plugAPI copied to clipboard
Use ES6 Promise in place of callbacks
I think this would be a beneficial change long term - 6.0?
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.
Do either v8's or bluebirds promisify work on PAPI yet? last time I checked they did not
In my opinion, using a standard feature of the platform is a benefit in and of itself.
As a kind of compromise, maybe methods that take a callback could return a promise if called without a callback argument?
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))
);
}
;)