0

I am building a wrapper function to retrieve values stored in a WebExtension's local Storage. I am struggeling to pass the return value of the success function to its outer function. Many thanks in advance!

 get(key) {

    browser.storage.local.get(key).then(onGot, onError);

    function onGot(storage) {

        console.log(storage[key]); // Works so far
    }

    function onError() {

        return false;
    }

    // How can I return storage[key] here?
}

1 Answers1

0

Either return a promise from the wrapper or let your promise resolve and consume the result with a callback.

A promise represents an operation which may or may not have completed yet. It provides hooks for success and failure so that the result of the async operation can be processed.

In order to return from the function, the caller and the executing function should stay in sync (w.r.t control flow). This moves away from async, which is what js promise is for.

Anuj Yadav
  • 980
  • 11
  • 20