来自 url 的同步 base64 编码
Synchronous base64 encode from url
我是异步 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
甚至没有 return 等待的 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 的新手,正在熟悉 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
甚至没有 return 等待的 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()));