使用chrome.storage.sync.get返回未定义的回调 [英] Callback returns undefined with chrome.storage.sync.get

查看:810
本文介绍了使用chrome.storage.sync.get返回未定义的回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var Options = function(){}; 

Options.prototype = {

getMode:function(){
return chrome.storage.sync.get(value,function(e){
console.log(e); //输出'Object {value:'test'}'。
return e;
});


setMode:function(){
chrome.storage.sync.set({value:test},function(e){
})
}
}

var options = new Options();
options.setMode();
console.log(options.getMode()); //打印'undefined'。

我预计它会打印



<$ p $当我打电话给我的时候,

Object {value:set up}

code> options.getMode(),但它打印 undefined



有人知道如何解决这个问题吗?

解决方案

chrome.storage API是异步 - 它不直接返回它,而是将其作为参数传递给回调函数。函数调用本身总是返回 undefined

通常用于允许其他方法运行,而无需等到某个事件响应或完成时 - 例如 setTimeout (唯一不同的是它返回一个计时器值,而不是 undefined )。 例如,取这个:
$ b

  setTimeout(function(){alert(1);},10000); 
alert(0);

因为 setTimeout 是异步的,所以不会停止所有的代码,直到整个函数完成,而不是最初返回,只有在稍后完成时才调用一个函数 - 这就是为什么0在1之前出现的原因。






出于这个原因,您不能简单地执行如下操作:

  //foo将永远是未定义的
var foo = asyncBar(function(e){return e;});

通常,您应该在回调函数中使用您想要执行的操作异步功能完成)。这是一种非常常见的编写异步代码的方式:

  function getValue(callback){
chrome.storage.sync .get(value,callback);
}

您可以将整段代码放入回调中 - 没有任何停止你这样做。因此,不要执行以下操作:

  console.log(getValue()); //典型的做同步方法

这可能是一个更好的主意:

  //如何在异步代码中完成
getValue(function(value){
console.log(value) ;
});


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

var Options = function(){};

Options.prototype = {

    getMode: function(){
               return chrome.storage.sync.get("value", function(e){  
                 console.log(e); // it prints 'Object {value: "test"}'.       
                 return e;
               });
    },

    setMode: function(){
        chrome.storage.sync.set({"value": "test"}, function(e) {         
        })
    }
}

var options = new Options();
options.setMode();
console.log(options.getMode()); // it prints 'undefined'.

I expected it to print

Object {value: "set up"}

when I call options.getMode(), but it prints undefined.

Does anyone know how to fix this problem?

解决方案

The chrome.storage API is asynchronous - it doesn't return it directly, rather passing it as an argument to the callback function. The function call itself always returns undefined.

This is often used to allow other methods to run without having to wait until something responds or completes - an example of this is setTimeout (only difference is that it returns a timer value, not undefined).

For example, take this:

setTimeout(function () { alert(1); }, 10000);
alert(0);

Because setTimeout is asynchronous, it will not stop all code until the entire function completes, rather returning initially, only calling a function when it is completed later on - this is why 0 comes up before 1.


For this reason, you cannot simply do something like:

// "foo" will always be undefined
var foo = asyncBar(function (e) { return e; }); 

Generally, you should put what you want to do in your callback (the function that is called when the asynchronous function is completed). This is a fairly common way of writing asynchronous code:

function getValue(callback) {
  chrome.storage.sync.get("value", callback);
}

You could place an entire portion of your code inside the callback - there's nothing stopping you from doing so. So instead of doing the following:

console.log(getValue()); // typical synchronous method of doing something

This would probably be a better idea:

// how it would be done in asynchronous code
getValue(function (value) {
  console.log(value);
}); 

这篇关于使用chrome.storage.sync.get返回未定义的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆