const jsonHash = async (input, init=0) => {
let result = {};
let resolvedResults = await Promise.all(
Object.entries(input).map(async ([key, value]) => {
if (value instanceof Function) value = value();
if (value instanceof Promise) value = await value;
return [key, value]
})
);
for (let [key, value] of resolvedResults) {
result[key] = value;
}
return result;
}
Usage
const name='Hash';
const hash = await jsonHash({
name,
post() {
return name;
},
async myNameIs() {
return `My Name is ${name}`;
},
});
console.log(hash.name, hash.post, hash.myNameIs);