I'm trying to use the result of a promise function in a non-asynchronous manner. It doesn't matter how long the calculation of the digest will take - I just want the execution of the code to wait (block, hibernate, stop, ...) until there is a result for the promise:
async function sha1(str) {
const r = await crypto.subtle.digest(`SHA-1`, new TextEncoder().encode(str));
return Array.prototype.map.call(new Uint8Array(r), x => x.toString(16).padStart(2, 0)).join(``);
}
function test(str) {
return sha1(str);
}
console.log(1);
console.log(test("abc"));
console.log(3);
console.log(test("def"));
As long as I don't go into details in the debugger, the resulting console-log is always in a "pending" state.
[Log] 1
[Log] Promise {status: "pending"}
[Log] 3
[Log] Promise {status: "pending"}
After opening the detail there is the correct result for each function call:
[Log] Promise
result: "a9993e364706816aba3e25717850c26c9cd0d89d"
status: "resolved"
...
I'm pretty sure I will get a "duplicate question" tag - but I couldn't find a satisfying answer. Sorry about that. Most of the examples just use an output function for demonstration purposes, but I need to return a value from the second function...