0

I'm having trouble make a save & load function, here's what I'd like to do :

function save(key,val){
    chrome.storage.sync.set({key:val}, function() {});

}

function load(key){
    chrome.storage.sync.get([key], function(result) {return result});
}

Problem is, by doing that, my save function really create this in storage : {key: "{"key":"val"}"}

The save takes function literally takes "key", instead of taking it as a parameter.

Also, if I want load tu return my value, I need to do "result.key" instead of "result", but same problem, it takes it literally, it doesn't looks at key as a parameter.

And last problem is, if I place my return here, it doesn't work, and return nothing.

Could anyone help me fix it and understand what I did wrong please ?

ps : thanks for the asynchronous part, it helped a bit, I'll use a callback for the returned value, but it doesn't answer the other problems.

  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – wOxxOm Oct 16 '18 at 08:03
  • 1
    Use `{[key]: val}`, see [How to use a variable for a key in a JavaScript object literal?](//stackoverflow.com/q/2274242) – wOxxOm Oct 16 '18 at 08:43
  • I tried but it didn't worked, I still got "undefined" when I load –  Oct 16 '18 at 09:21
  • You need to rework your code as shown in the [first link](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). – wOxxOm Oct 16 '18 at 09:24
  • I tried outside of functions, just to test your solution –  Oct 16 '18 at 09:27
  • it worked with result[key] in the load part –  Oct 16 '18 at 09:41

0 Answers0