-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Joe Wemyss
committed
Dec 17, 2017
1 parent
bee44dd
commit 29a763a
Showing
1 changed file
with
13 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,24 @@ | ||
[![Coverage Status](https://coveralls.io/repos/github/JavaTheNutt/error-first-emulator/badge.svg?branch=master)](https://coveralls.io/github/JavaTheNutt/error-first-emulator?branch=master) | ||
|
||
# Error First Emulator | ||
_simple, lightweight, dependency-free library to emulate error-first handling in async/await scenarios_ | ||
|
||
This library is designed with the purpose of emulating error first callback handling in async/await scenarios. Imagine a scenario where you make an asynchronous call to a third party API. With callbacks, the error would have been returned as the first parameter, with a Promise, it would have been rejected. With async/await operation, it must be thrown. Take the following example: | ||
This library is designed with the purpose of emulating error first callback handling in async/await scenarios. | ||
Since errors error handling can now be managed using `try/catch` blocks, it can be difficult to ascertain whether a function called earlier has completed successfully, since we no longer have access to an error first callback, or a catch block within a Promise. This is where this library steps in. It provides a simple method of encapsulating any errors inside an object. Then you can check the result using `result.error`. See the example below | ||
|
||
**Callbacks** | ||
**Usage** | ||
```js | ||
const errorHandler = require('error-first-emulator'); | ||
const someAsynchronousFunction = async () => { | ||
try{ | ||
await doSomethingElse(); | ||
return await doSomethingElse(); | ||
}catch (error){ | ||
//How do we | ||
return errorHandler.errorFormatter('something went wrong while doing something else', error); | ||
} | ||
} | ||
const someOtherAction = async () => { | ||
const result = await someAsynchronousFunction(); | ||
if(result.error) return result; //pass error back up the stack | ||
doSomethingWith(result); | ||
} | ||
``` |