1

Let's say I have the following function

chrome.cookies.getAll({domain: 'google.com'}, callbackFn);

where callbackFn should return all the cookies available

now I have a big main() function (I must have this function) and this function must return the cookies of the given page(as a promise).

How should I do it?

Novellizator
  • 13,633
  • 9
  • 43
  • 65

1 Answers1

0

You can do

function main(){
    return new Promise(function (resolve/*, reject*/) {
         chrome.cookies.getAll({domain: 'google.com'}, resolve);
    });
}

You'd use it like this:

main().then(function(cookies) {
     // eat cookies
});
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • this is something like custom .promisify() method? I tried that, but it threw an error... – Novellizator May 13 '15 at 11:44
  • What threw an error ? `new Promise` ? What error ? – Denys Séguret May 13 '15 at 11:47
  • Unhandled rejection (<[{"domain":".l.facebook.com","expirati...>, no stack trace) (tried on "facebook.com" instead of "google.com")... The Bluebird.promisify(chrome.cookie.getAll) threw that. now i'm gonna try your solution :) – Novellizator May 13 '15 at 11:49
  • @Novellizator It's normal: `promisify` expects the passed function to return an error as first argument to the callback. So the array of cookies was interpreted as an error by BlueBird (which means you could have got it in using `throw` if you wanted to play real dirty) – Denys Séguret May 13 '15 at 11:51
  • 1
    @Novellizator `promisify` will only work if the callback is in the form `err, result` – t.niese May 13 '15 at 11:51
  • @dystroy now I get Unhandled rejection TypeError: Cannot read property 'getAll' of undefined. Have the same code only instead of new Promise I have new Bluebird. What is the matter? :( – Novellizator May 13 '15 at 11:59
  • What ? Are you testing in a Chrome extension ? – Denys Séguret May 13 '15 at 12:01
  • oh sorry, wait. chrome.cookieS is the right way. my fault, thanks – Novellizator May 13 '15 at 12:02
  • Oh yes, didn't check the spelling when copy-pasting – Denys Séguret May 13 '15 at 12:02