Javascript 如何使用chrome.storage.local.get和XMLHttpRequest同步执行函数?
我正在使用上述函数从本地存储中检索某些参数,如果它们不在本地存储中,则Javascript 如何使用chrome.storage.local.get和XMLHttpRequest同步执行函数?,javascript,jquery,google-chrome,google-chrome-extension,Javascript,Jquery,Google Chrome,Google Chrome Extension,我正在使用上述函数从本地存储中检索某些参数,如果它们不在本地存储中,则chrome.storage.local.get回调会向服务器发送数据请求。这使得整个get_pps_params异步(显然)。那么如何使它同步执行呢?我读了一些关于jQuery的东西,但不理解它 当前回调(ppsParams)由于XMLHttpRequest的异步性质,如果请求被发送到服务器,则返回null。类似于上次的解决方案 function get_pps_params(callback) { var ppsP
chrome.storage.local.get
回调会向服务器发送数据请求。这使得整个get_pps_params
异步(显然)。那么如何使它同步执行呢?我读了一些关于jQuery的东西,但不理解它
当前
回调(ppsParams)
由于XMLHttpRequest的异步性质,如果请求被发送到服务器,则返回null。类似于上次的解决方案
function get_pps_params(callback)
{
var ppsParams = null;
chrome.storage.local.get(['ppsParams'], function(result) {
if ($.isEmptyObject(result))
{
var oReq = new XMLHttpRequest();
oReq.open("GET", CLOUD_SERVER + 'get_pps_params', true);
oReq.responseType = "arraybuffer";
oReq.onload = function (oEvent) {
console.log("Got pps params compressed!");
ppsParams = oReq.response; // Note: not oReq.responseText
chrome.storage.local.set({ppsParams: _arrayBufferToBase64(ppsParams)});
};
oReq.send();
}
else
{
ppsParams = _base64ToArrayBuffer(result.ppsParams);
}
callback(ppsParams);
});
}
如果您真的想使用jquery.Deferred
function get_pps_params(callback)
{
var ppsParams = null;
chrome.storage.local.get(['ppsParams'], function(result) {
if ($.isEmptyObject(result))
{
var oReq = new XMLHttpRequest();
oReq.open("GET", CLOUD_SERVER + 'get_pps_params', true);
oReq.responseType = "arraybuffer";
oReq.onload = function (oEvent) {
console.log("Got pps params compressed!");
ppsParams = oReq.response; // Note: not oReq.responseText
chrome.storage.local.set({ppsParams: _arrayBufferToBase64(ppsParams)});
callback(ppsParams); // added this line
};
oReq.send();
}
else
{
ppsParams = _base64ToArrayBuffer(result.ppsParams);
callback(ppsParams); // moved this line
}
});
}
而不是
function get_pps_params()
{
var ppsParams = null;
var def = jQuery.Deferred();
chrome.storage.local.get(['ppsParams'], function(result) {
if ($.isEmptyObject(result))
{
var oReq = new XMLHttpRequest();
oReq.open("GET", CLOUD_SERVER + 'get_pps_params', true);
oReq.responseType = "arraybuffer";
oReq.onload = function (oEvent) {
console.log("Got pps params compressed!");
ppsParams = oReq.response; // Note: not oReq.responseText
chrome.storage.local.set({ppsParams: _arrayBufferToBase64(ppsParams)});
def.resolve(ppsParams);
};
oReq.send();
}
else
{
ppsParams = _base64ToArrayBuffer(result.ppsParams);
def.resolve(ppsParams);
}
});
return def.promise();
}
使用
您可以使用适当的Promise-comes标准来实现相同的结果,对于任何体面的浏览器,或者对于不体面的浏览器,使用polyfill
再一次,你会使用
function get_pps_params()
{
return new Promise(function(resolve, reject) {
var ppsParams = null;
chrome.storage.local.get(['ppsParams'], function(result) {
if ($.isEmptyObject(result))
{
var oReq = new XMLHttpRequest();
oReq.open("GET", CLOUD_SERVER + 'get_pps_params', true);
oReq.responseType = "arraybuffer";
oReq.onload = function (oEvent) {
console.log("Got pps params compressed!");
ppsParams = oReq.response; // Note: not oReq.responseText
chrome.storage.local.set({ppsParams: _arrayBufferToBase64(ppsParams)});
resolve(ppsParams);
};
oReq.send();
}
else
{
ppsParams = _base64ToArrayBuffer(result.ppsParams);
resolve(ppsParams);
}
});
});
}
方法@JaromandaX是的,我之前只是问了这个问题,但现在情况有所不同。是的,这应该行得通,尽管我想知道jquery。更多。谢谢:)我不知道我会为jquery而烦恼。也可以推迟。即使我想使用jQuery API而不是本机XHR,我也会使用成功处理程序。如果您将其重写为使用jQuery.Deferred,则不会使用回调,而是返回一个承诺,并使用
get\u pps\u params()调用它。然后(…
不要使用jquery使用标准js promise。@ZigMandel-只要你有一个像样的浏览器,IE就不需要应用
get_pps_params().then(someFunc);
function get_pps_params()
{
return new Promise(function(resolve, reject) {
var ppsParams = null;
chrome.storage.local.get(['ppsParams'], function(result) {
if ($.isEmptyObject(result))
{
var oReq = new XMLHttpRequest();
oReq.open("GET", CLOUD_SERVER + 'get_pps_params', true);
oReq.responseType = "arraybuffer";
oReq.onload = function (oEvent) {
console.log("Got pps params compressed!");
ppsParams = oReq.response; // Note: not oReq.responseText
chrome.storage.local.set({ppsParams: _arrayBufferToBase64(ppsParams)});
resolve(ppsParams);
};
oReq.send();
}
else
{
ppsParams = _base64ToArrayBuffer(result.ppsParams);
resolve(ppsParams);
}
});
});
}
get_pps_params().then(someFunc);