我想在瀏覽器擴展中使用Fetch API下載資源並計算其哈希值。下面的工作(使用crypto通過Browserify)獲取資源,計算哈希,返回承諾
fetch(url).then(function(response) {
return response.blob();
}).then(function(data) {
var a = new FileReader();
a.readAsBinaryString(data);
a.onloadend = function() {
var hash = crypto.createHash(hashType);
hash.update(a.result, 'binary');
return hash.digest('hex');
};
})
,但有一個缺點,我不得不等待a.onloadend
而在這我想嵌入的背景下,需要將返回Promise
。此外,首先讀取整個blob,然後將其讀入FileReader
,然後將其轉儲到createHash
似乎很奇怪。
任何提示?
也許這個問題會更適合HTTP: //codereview.stackexchange.com? – Pavlo
這個問題看起來很適合[Code Review.SE](http://codereview.stackexchange.com/),前提條件是(a)你希望檢查代碼的每個方面,而不僅僅是一些, (b)你的代碼已經在工作_(c)你正在要求檢查_concrete,真正的code_,而不是抽象的設計(不管它是否被表示爲代碼)。如果您同意所有這些內容,請閱讀[主題內容](http://codereview.stackexchange.com/help/on-topic),如果您的問題符合要求,請在此處將其刪除並重新發布到CR 。 – Phrancis
(a)和(c)在這裏不適用,所以我想這不適合。 –