Skip to content

Commit

Permalink
updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Wemyss committed Dec 17, 2017
1 parent bee44dd commit 29a763a
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions README.md
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);
}
```

0 comments on commit 29a763a

Please sign in to comment.