google.chrome.storage undefined输出

时间:2016-06-30 01:08:53

标签: javascript google-chrome-extension

我从chrome.storage获得了这个奇怪的输出

当我在这里试用这段代码时

var local;
chrome.storage.local.get(function(data) {
    local = data;
    console.log(local);
});
console.log('>');
console.log(local);
console.log('<');

这里是输出(dataString是数据的值)

>
undefined
<
dataString

在&gt;之前应该打印dataString。 undefined&lt;对?或许我只是困惑

1 个答案:

答案 0 :(得分:2)

正如chrome storage docs中所述,chrome.storage是异步的,因此#!/usr/bin/env python import sys import re from collections import defaultdict #loop through args for thisarg in sys.argv[1:]: #initialize a defualt dict bysign = defaultdict(list) #read the file try: thisfile = open(thisarg,'r') for line in thisfile: #split each line by space and colon dat = re.split('[ :]*',line.strip()) #append line to dictionary indexed by ENSG code bysign[dat[-2]].append(line.strip()) thisfile.close() except IOError: print "no such file {:}".format(thisarg) #extract the keys from the dictionary mykeys = bysign.keys() #sort the keys mykeys.sort() for key in mykeys: #initialize another, smaller dictionary bytuple = dict() #loop through all the lines that have the same ENSG code group = bysign[key] for line in group: #extract the E/I code ei=line.split(':')[-1] #convert the E/I code to a (char,int) tuple letter = ei[0] number = int(ei[1:]) #use that tuple to index the smaller dict bytuple[(letter,number)] = line #extract the keys from the sub-dictionary eikeys = bytuple.keys() #sort the keys eikeys.sort() #print the results for k in eikeys: print bytuple[k] 的回调函数会在稍后执行,这就是你获得该输出的原因。