Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,31 @@ app.get('/compute-with-set-immediate', async function computeWSetImmediate(req,
res.send(hash.digest('hex') + '\n');
});

app.get('/compute-with-set-immediate-closure', function computeWSetImmediate(req, res) {
function asyncUpdateHash(n, doneCB) {
const hash = crypto.createHash('sha256');
// Update ongoing hash in JS closure.
function updateHash(i, cb) {
hash.update(randomString());
if (i == n) {
cb(hash);
return;
}

// "Asynchronous recursion".
// Schedule next operation asynchronously.
setImmediate(updateHash.bind(null, i+1, cb));
}

// Start the helper, with CB to call doneCB.
updateHash(1, doneCB);
}

asyncUpdateHash(10e6, function(hash){
res.send(hash.digest('hex') + '\n');
});
});

app.get('/compute-with-next-tick', async function computeWNextTick (req, res) {
log('computing async with nextTick!');

Expand Down