Callback returns undefined with chrome.storage.sync.get

后端 未结 3 663
滥情空心
滥情空心 2020-12-06 06:14

I\'m building a Chrome extension and I wrote this code.

var Options = function(){};

Options.prototype = {

    getMode: function(){
               return ch         


        
3条回答
  •  天命终不由人
    2020-12-06 06:54

    The chrome.storage.sync.get is called asynchronously, that's why you have to pass it a callback, so that it is executed in the future.

    When you try to print the returned value of getModeyou are printing the return value of whatever chrome.storage.sync.get returns after queuing the asynchronous call to be executed.

    This is a common mistake people do in javascript, while they are learning to use asynch calls.

提交回复
热议问题