0
タイミングの問題があります。何が起こっているのは、chrome.storage.local.get
が呼び出される前にtoDataUrlが完了しないことです。 dataUrlの準備ができたら、どのようにリファクタリングしてマップや他のループを動かすことができますか?私は非同期を待って、forEachをマップに変更したり、その逆に変更したりしました。 whileループとカウンタを試してみましたが、正しく実装していないのでしょうか?約束を待っている間にmap/forEachのタイミング問題
明確にするには、現時点では発生していないすべてのループが完全に完了したら、chrome.storage.local.get
を呼び出す必要があります。データURLに
:
const toDataURL = url => fetch(url)
.then(response => response.blob())
.then(blob => new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onloadend =() => resolve(reader.result)
reader.onerror = reject
reader.readAsDataURL(blob)
}))
本体:
app.checkOfflineStorage = function(quotes) {
let nQuotes = quotes;
nQuotes.data.map(function(e, idx) {
if(e.attachments) {
e.attachments.data.forEach(function(el) {
if(el.type === 'quote-picture') {
toDataURL(`https://quotecatalog.imgix.net${el.value}?w=110`).then(function(res) {
el.dataURI = res;
})
}
})
}
if(e.title) {
if(typeof e.title.data.attachments !== 'undefined') {
var title_attachments = e.title.data.attachments.data;
title_attachments.forEach(function(el) {
toDataURL(`https://quotecatalog.imgix.net${el.value}?w=110`).then(function(res) {
el.dataURI = res;
})
})
}
}
})
chrome.storage.local.get('offlineCache',
if (Object.keys(storedQuoteArray).length === 0) {
console.log(nQuotes)
chrome.storage.local.set({"offlineCache": nQuotes}, function(res) {});
} else {
console.log(nQuotes)
var combinedArray = storedQuoteArray.offlineCache.data.concat(nQuotes.data)
combinedArray = combinedArray.slice(0, 100)
console.log('100', combinedArray)
chrome.storage.local.set({"offlineCache": {"data": combinedArray}}, function(res) {});
}
})
}
はあなたが達成しようとしているものに関して、手の込んだてもらえますか? 'chrome.set.storage'への不思議な参照はあなたのコードには表示されないので、何の鐘も鳴りません。 So:あなたは何を達成しようとしていますか?ここで期待される行動は何ですか?現在の行動はどう違うのですか? – deiga
@deiga質問を更新しました – Quesofat
.map(なぜ何も返さない場合は.mapまたは.forEach "ループ")のいずれかであなたの約束が待たれていません –