工作嘗試使用異步/地等待着forEach
異步/伺機不在的forEach
export let appState = observable({
bunny : []
});
appState.loadBunny = async function(bugs) {
bugs.forEach(function(data) {
let temp = {};
temp['id'] = data.id;
temp['site_url'] = data.site_url;
temp['email'] = await decrypt(sessionStorage.getItem('key'), data.email);
temp['username'] = await decrypt(sessionStorage.getItem('key'), data.username);
temp['password'] = await decrypt(sessionStorage.getItem('key'), data.password);
temp['note'] = await decrypt(sessionStorage.getItem('key'), data.note);
temp['tag'] = await decrypt(sessionStorage.getItem('key'), data.tag);
temp['created_at'] = data.created_at;
temp['updated_at'] = data.updated_at;
runInAction("update state after decrypting data",() => {
this.bunny.push(temp);
});
});
};
appState.fetch = async function() {
let xoxo = await axios.get('/api/vault/', {
headers: {'Authorization': "JWT " + sessionStorage.getItem('token')}
});
this.loadBunny(xoxo.data);
}
時獲得意外的標記,這裏是錯誤:
ERROR in ./static/apps/store/passwords.js
Module build failed: SyntaxError: ...static/apps/store/passwords.js: Unexpected token (15:30)
13 | temp['id'] = data.id;
14 | temp['site_url'] = data.site_url;
> 15 | temp['email'] = await decrypt(sessionStorage.getItem('key'), data.email);
| ^
16 | temp['username'] = await decrypt(sessionStorage.getItem('key'), data.username);
您只能在'async'函數中「await」。 – zerkms