Skip to content

Instantly share code, notes, and snippets.

@codenirvana
Created July 16, 2020 17:41
Show Gist options
  • Save codenirvana/0f113095863fb289c2bdd46d6cd5f057 to your computer and use it in GitHub Desktop.
Save codenirvana/0f113095863fb289c2bdd46d6cd5f057 to your computer and use it in GitHub Desktop.
Safe Promise
Promise.prototype.safe = function () {
return this.then(data => {
if (data instanceof Error) return [data];
return [null, data];
}).catch(err => [err]);
}
// const [err, result] = await promise.safe();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment