使用 Promises 重写 Javascript 函数
Rewrite Javascript Function using Promises
我坚持更新我的代码以现在使用 promises。
散列函数正在运行,但已更改。它现在 returns 是一个承诺,不再是一个价值。而且我不知道如何使用它:-(
function hash(input) {
// transform the input into an arraybuffer.
let buffer = new TextEncoder("utf-8").encode( JSON.stringify(input) );
return window.crypto.subtle.digest("SHA-256", buffer).then( (hash)=> {
return btoa(String.fromCharCode( ...new Uint8Array(hash) ));
});
}
这是需要重写的函数。 hash(...) 被调用了几次。
function update(){
// array of Objects (empty to simplify my question)
let data = [{}, {}, {}];
// if data has changed => process it
if( hash(data) !== hash(previousData) ){
// clone for next time comparison (before calculating hashes!)
previousData = JSON.parse(JSON.stringify(data)); // simple deepclone
data.forEach( item=>{
// reset dynamic values within a cloned item for simple item comparison checks
item.id = hash( Object.assign( {}, item, {dynValue:1}) );
//complet item hash including dynamic values
item.hash = hash( item );
});
// fire event with changed data array
// ...
}
}
我基本上是为数组中的每个对象创建两个散列以简化以后的比较。一种具有一些忽略的属性(重置为默认值),另一种用于完整对象。
谢谢你帮助我
你的比较必须改变,因为你现在正在编写异步代码。比较必须等到对 hash
的两次调用都解决。
示例代码:
var hash = hash(data);
var hashPrev = hash(previousData);
hash.then((hashVal) => {
hashPrev.then((hashPrevVal) => {
if (hashVal == hashPrevVal) doFurtherLogicHere();
});
});
理想情况下,您希望尝试保留散列函数调用的直接性质,因为更改它需要进行如此大的重写,但是如果没有这种情况,您可能会做一些像 Promise...然后链.
function update() {
//Same code
Promise.all([hash(data), hash(previousData)]), then(values => {
if (values[0] !== values[1]) {
//Same code
var promiseChain = [];
data.forEach(item=> {
promiseChain.push(
Promise.all([
hash(Object.assign({}, item, {dynValue:1})),
hash(item)
]).then(values=> {
item.id = values[0];
item.hash = values[1];
})
);
});
Promise.all(promiseChain).then(/* fire event with changed data array */);
}
}
}
我坚持更新我的代码以现在使用 promises。
散列函数正在运行,但已更改。它现在 returns 是一个承诺,不再是一个价值。而且我不知道如何使用它:-(
function hash(input) {
// transform the input into an arraybuffer.
let buffer = new TextEncoder("utf-8").encode( JSON.stringify(input) );
return window.crypto.subtle.digest("SHA-256", buffer).then( (hash)=> {
return btoa(String.fromCharCode( ...new Uint8Array(hash) ));
});
}
这是需要重写的函数。 hash(...) 被调用了几次。
function update(){
// array of Objects (empty to simplify my question)
let data = [{}, {}, {}];
// if data has changed => process it
if( hash(data) !== hash(previousData) ){
// clone for next time comparison (before calculating hashes!)
previousData = JSON.parse(JSON.stringify(data)); // simple deepclone
data.forEach( item=>{
// reset dynamic values within a cloned item for simple item comparison checks
item.id = hash( Object.assign( {}, item, {dynValue:1}) );
//complet item hash including dynamic values
item.hash = hash( item );
});
// fire event with changed data array
// ...
}
}
我基本上是为数组中的每个对象创建两个散列以简化以后的比较。一种具有一些忽略的属性(重置为默认值),另一种用于完整对象。
谢谢你帮助我
你的比较必须改变,因为你现在正在编写异步代码。比较必须等到对 hash
的两次调用都解决。
示例代码:
var hash = hash(data);
var hashPrev = hash(previousData);
hash.then((hashVal) => {
hashPrev.then((hashPrevVal) => {
if (hashVal == hashPrevVal) doFurtherLogicHere();
});
});
理想情况下,您希望尝试保留散列函数调用的直接性质,因为更改它需要进行如此大的重写,但是如果没有这种情况,您可能会做一些像 Promise...然后链.
function update() {
//Same code
Promise.all([hash(data), hash(previousData)]), then(values => {
if (values[0] !== values[1]) {
//Same code
var promiseChain = [];
data.forEach(item=> {
promiseChain.push(
Promise.all([
hash(Object.assign({}, item, {dynValue:1})),
hash(item)
]).then(values=> {
item.id = values[0];
item.hash = values[1];
})
);
});
Promise.all(promiseChain).then(/* fire event with changed data array */);
}
}
}