I'm running into a little trouble in my promise code where my Promise.map seems to end prematurely. I'm sure it's a problem with my logic but with the code I'm unsure how to debug it.
Currently, it outputs [], B, A, A
with a blobList
of size 2. How can I have it so that it finishes pushing to imgList
and thus outputs A, A, [*some base64 encoding here*], B
where in the .then
function, imgList should not be empty?
blobToBase64 Function
blobToBase64(blob, cb) {
var reader = new window.FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
let base64data = reader.result;
cb(base64data);
}
}
Main function
Promise.map(blobList, function(blobObj){
helper.blobToBase64(blobObj.data, (fn) => {
imgList.push(fn);
console.log('A');
})
}).then(function(){
console.log(imgList);
console.log('B');
})
Here you go:
var promises = blobList.map((blobObj) => {
return new Promise((resolve, reject) => {
helper.blobToBase64(blobObj.data, (fn) => {
imgList.push(fn);
console.log('A');
return resolve();
})
})
})
Promise
.all(promises)
.then(() => {
console.log(imgList);
console.log('B');
})