2018-10-10 15:05:20 +00:00
|
|
|
/* exported promisify */
|
2018-10-04 17:03:40 +00:00
|
|
|
'use strict';
|
2018-10-10 15:05:20 +00:00
|
|
|
/*
|
|
|
|
Convert chrome APIs into promises. Example:
|
2018-10-04 17:03:40 +00:00
|
|
|
|
2018-10-10 15:05:20 +00:00
|
|
|
const storageSyncGet = promisify(chrome.storage.sync.get.bind(chrome.storage.sync));
|
|
|
|
storageSyncGet(['key']).then(result => {...});
|
|
|
|
|
|
|
|
*/
|
2018-10-04 17:03:40 +00:00
|
|
|
function promisify(fn) {
|
|
|
|
return (...args) =>
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
fn(...args, (...result) => {
|
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
reject(chrome.runtime.lastError);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve(
|
|
|
|
result.length === 0 ? undefined :
|
|
|
|
result.length === 1 ? result[0] : result
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|