This question has already been answered:

There is a question that has tormented me for a long time. Functions like chrome.storage.sync have some kind of protection mechanism for something or just in the field of visibility, it is not clear.

I am writing an application, I have already asked questions about the method of saving data, but it is still necessary to reduce the code so that functions like chrome.storage.sync.get() or chrome.storage.sync.set() be protected into separate functions in something like designs:

 function get_option_storage(option_name) { var result = false; chrome.storage.sync.get( option_name, function(func_result) { result = func_result; }); return result; } 

and for installation

 function set_option_storage(option_name, option_value) { var result = false; chrome.storage.sync.set( {option_name: option_value}, function() { result = true; }); return result; } 

I came to this because of the high number of requests to save and read data from the repository, so I want to sew up methods of working with the repository into separate functions and call my own, how can this be done and is it possible at all?

Reported as a duplicate by members Pavel Mayorov , Vadim Ovchinnikov , user194374, Qwertiy , Grundy javascript Jan 20 '17 at 5:54 pm .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

    Almost all chrome.* Methods that have callbacks are asynhorn. Accordingly, asynhorn methods do not return any data. This is not a protection mechanism or even an area of ​​visibility - it is a feature that was introduced to increase the speed of JavaScript scripts.

    About what is asynchronous methods, I, as it seems to me, clearly explained in the answer to this question: Search for elements on the page and API VK . If interested, read.

    What can be done:

    1. Use Promises. Articles on this topic:

    2. Use the Mozilla polyfill solution: mozilla / webextension-polyfill . It's a bit damp, but adding support for Promises is pretty easy with it. Please note that this polyfill does not cover all methods (some unpopular methods will have to be added with pens), and also slightly changes the API.

    But from custom functions like get *, which return something via return , you will still have to refuse. The only thing I can advise is to rebuild the structure of the application or use callbacks instead of return .