Node-jsonwebtoken: Synchronous vs Asynchronous

Created on 9 Jan 2019  路  9Comments  路  Source: auth0/node-jsonwebtoken

Synchronous

var jwt = require('jsonwebtoken');
var token = jwt.sign({ foo: 'bar' }, 'shhhhh');

Asynchronous

var token = jwt.sign({ foo: 'bar' }, 'shhhhh', function(err, token) {
  console.log(token);
);

I want to know , is it bad to use Synchronous way , in express js server? is there any benefit with Asynchronous mode? As I think there is no I/O and just CPU usage, So Asynchronous and Synchronous must b same, just syntax is different

Most helpful comment

TL;DR; To allow consumers load keys from filesystem or any other source of ReadStream without blocking event loop. Not to actually perform the signing.

I've been digging into git history and jws implementation to understand why the library provided the async functionality for signing:

Honestly, not sure if that streaming is a desired feature by consumers, we'd need to update the README to make it clear.

All 9 comments

@ziluvatar , @MitMaro can you help me?

Hello???????????

:-((

https://medium.com/@patrykcieszkowski/jwt-authentication-in-express-js-ee898b87a60 section Verifying the Token may help you

@wjureczkaPracuj as I understand, in point of performance there is no different I use sync or async mode, and it is safe to use sync mode in server too

in point of performance there is no different I use sync or async mode, and it is safe to use sync mode in server too

Correct. Although if you use the sync way be sure you wrap it with try-catch.

If there is no perf difference, why does the library offer an async alternative?

TL;DR; To allow consumers load keys from filesystem or any other source of ReadStream without blocking event loop. Not to actually perform the signing.

I've been digging into git history and jws implementation to understand why the library provided the async functionality for signing:

Honestly, not sure if that streaming is a desired feature by consumers, we'd need to update the README to make it clear.

@ziluvatar That sounds like some serious coding archaeology... well done! :wink:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

BarukhOr picture BarukhOr  路  4Comments

glowlabs picture glowlabs  路  3Comments

itamarwe picture itamarwe  路  3Comments

rockchalkwushock picture rockchalkwushock  路  4Comments

samholmes picture samholmes  路  5Comments