Skip to content

Instantly share code, notes, and snippets.

@Karnak19
Created January 20, 2021 08:20
Show Gist options
  • Select an option

  • Save Karnak19/8f86d71db2517ff27676ac9a3f4441d0 to your computer and use it in GitHub Desktop.

Select an option

Save Karnak19/8f86d71db2517ff27676ac9a3f4441d0 to your computer and use it in GitHub Desktop.
Async JWT sign example
const asyncJwtVerify = (token, privateKey) => {
return new Promise((resolve, reject) => {
jwt.verify(token, privateKey, (err, decoded) => {
if (err) {
reject(err);
}
resolve(decoded);
});
});
};
// Promise example
asyncJwtVerify(token, PRIVATE_KEY)
.then((decoded) => {
console.log(decoded);
// Do something when it works...
})
.catch((error) => {
console.error(error);
// Do something when it doesn't work
});
// Async/await example
(async () => {
try {
const decoded = await asyncJwtVerify(token, PRIVATE_KEY);
console.log(decoded);
// Do something when it works...
} catch (error) {
console.log(error);
// Do something when it doesn't work
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment