0

I am unable to understand Google chromes get and set functions.

Please refer the below code snippet.

    var v1 = 'l1';
    var v2 = 'l2';

    storage.set({v1:'s1'});

    storage.get('v1',function(result){
        console.log(v1,result.v1);
    });

    storage.get('v2',function(result){
        console.log(v2,result.v2);
    });

I got the following output. l1 s1 l2 s1

I am purposely not setting v2 as I would like to understand what the result is when the variable hasn't been set yet.

I am not able to follow why get with key v2 returns s1.

Can you somebody pplease let me know where I am going wrong.

Regards, Farhan

Farhan
  • 411
  • 1
  • 10
  • 23
  • 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 Apr 19 '18 at 18:09
  • Chrome API is asynchronous so you need to chain callbacks or use Promise (for example Mozilla's WebExtension polyfill for Chrome). – wOxxOm Apr 19 '18 at 18:10

0 Answers0