我是异步 JavaScript 的新手,并且正在熟悉 async/await 的执行方式。 但我面临一个问题。
我正在尝试使用 xhr 获取图像并将其转换为 base64,将结果传递给回调。
但是我在回调中的控制台日志出现在应该出现在末尾的控制台日志之后。
我尝试了很多方法,但无法弄清楚我做错了什么。我只是希望代码的执行是同步的。
我们将非常感谢一些帮助。
这是我的代码:
function toDataUrl(url, callback) {
var xhr = new XMLHttpRequest();
xhr.onload = async function() {
var reader = new FileReader();
reader.onloadend = async function() {
await callback(reader.result);
};
reader.readAsDataURL(xhr.response);
};
xhr.open('GET', url);
xhr.responseType = 'blob';
xhr.send();
}
for (const element of data.elements) { // This is inside an async function
let index_elem = tabMission.findIndex(x => x.id == element.id);
if (index_elem === -1) {
let codes = [];
$.each(element.codes, (code, position) => {
codes.push({ code: code, position: position, isSaved: false });
});
window.localStorage.setItem('photos', JSON.stringify([]));
for (const photo of element.photos) {
await toDataUrl(
base_url + 'storage/images/elements/' + photo,
async result => {
let photos = JSON.parse(
window.localStorage.getItem('photos')
);
photos.push(result);
console.log(JSON.stringify(photos));
window.localStorage.setItem(
'photos',
JSON.stringify(photos)
);
}
);
}
//setTimeout(() => {
console.log(JSON.parse(window.localStorage.getItem('photos'))); // This prints at the end of logs
tabMission.push({
id: element.id,
title: element.title,
codes: codes,
photos: JSON.parse(window.localStorage.getItem('photos')),
toSynchronize: false
});
setTabMissionById(tabMission, request['mission_id']);
// }, 5000);
}
}
console.log(getTabMissionById($('#mission_id').val())); // This should print after all logs
最佳答案
您在开始时过度使用了 async/await,并且 toDataUrl
甚至没有返回 Promise 来等待 - 这就是您的序列失败的地方
function toDataUrl(url) {
return new Promise((resolve, reject) => {
var xhr = new XMLHttpRequest();
xhr.onload = function () {
var reader = new FileReader();
reader.onloadend = function () {
resolve(reader.result);
};
reader.readAsDataURL(xhr.response);
};
xhr.onerror = reject;
xhr.open('GET', url);
xhr.responseType = 'blob';
xhr.send();
});
}
for (const element of data.elements) { // This is inside an async function
let index_elem = tabMission.findIndex(x => x.id == element.id);
if (index_elem === -1) {
let codes = [];
$.each(element.codes, (code, position) => {
codes.push({
code: code,
position: position,
isSaved: false
});
});
window.localStorage.setItem('photos', JSON.stringify([]));
for (const photo of element.photos) {
const result = await toDataUrl(base_url + 'storage/images/elements/' + photo);
let photos = JSON.parse(window.localStorage.getItem('photos'));
photos.push(result);
console.log(JSON.stringify(photos));
window.localStorage.setItem('photos',JSON.stringify(photos));
}
console.log(JSON.parse(window.localStorage.getItem('photos'))); // This prints at the end of logs
tabMission.push({
id: element.id,
title: element.title,
codes: codes,
photos: JSON.parse(window.localStorage.getItem('photos')),
toSynchronize: false
});
setTabMissionById(tabMission, request['mission_id']);
// }, 5000);
}
}
console.log(getTabMissionById($('#mission_id').val()));
关于javascript - 从 url 同步进行 base64 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58009393/