All Projects → fedor → co_demo

fedor / co_demo

Licence: MIT license
A step-by-step guide about how to avoid callback hell with ES6 Promises + generators (aka make your own "co")

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to co demo

retryx
Promise-based retry workflow library.
Stars: ✭ 21 (+23.53%)
Mutual labels:  promise, error-handling
lightflow
A tiny Promise-inspired control flow library for browser and Node.js.
Stars: ✭ 29 (+70.59%)
Mutual labels:  promise, flow-control
Await Of
await wrapper for easier errors handling without try-catch
Stars: ✭ 240 (+1311.76%)
Mutual labels:  promise, error-handling
of
🍬 Promise wrapper with sugar 🍬
Stars: ✭ 13 (-23.53%)
Mutual labels:  promise, error-handling
Log Process Errors
Show some ❤️ to Node.js process errors
Stars: ✭ 424 (+2394.12%)
Mutual labels:  promise, error-handling
Await Handler
Basic wrapper for await that allows handling of errors without try/catch blocks
Stars: ✭ 13 (-23.53%)
Mutual labels:  promise, error-handling
parley
Flow control harness for implementors. Builds a Deferred object that supports async/await, promise chaining, and conventional Node callbacks.
Stars: ✭ 23 (+35.29%)
Mutual labels:  promise, flow-control
ency
Enhanced concurrency primitives for Javascript.
Stars: ✭ 32 (+88.24%)
Mutual labels:  flow-control
rakered
The open source components from rake.red
Stars: ✭ 28 (+64.71%)
Mutual labels:  error-handling
async
Synchronization and asynchronous computation package for Go
Stars: ✭ 104 (+511.76%)
Mutual labels:  promise
woodpecker
woodpecker http client for Android
Stars: ✭ 17 (+0%)
Mutual labels:  promise
rocket-pipes
Powerful pipes for TypeScript, that chain Promise and ADT for you 🚌 -> ⛰️ -> 🚠 -> 🏂 -> 🚀
Stars: ✭ 18 (+5.88%)
Mutual labels:  promise
pony-cause
Ponyfill and helpers for the standardized Error Causes
Stars: ✭ 42 (+147.06%)
Mutual labels:  error-handling
ignition-stackoverflow
An Ignition tab that fetches StackOverflow questions and provides a searchbar.
Stars: ✭ 74 (+335.29%)
Mutual labels:  error-handling
redux-airbrake
Redux middleware for Airbrake error logging
Stars: ✭ 20 (+17.65%)
Mutual labels:  error-handling
vue2-element
基于vue2 + vue-router2 + element-ui + vuex2 + fetch + webpack2 企业级后台管理系统最佳实践
Stars: ✭ 115 (+576.47%)
Mutual labels:  promise
miniprogram-network
Redefine the Network API of Wechat MiniProgram (小程序网络库)
Stars: ✭ 93 (+447.06%)
Mutual labels:  promise
indexeddb-orm
Indexed DB ORM
Stars: ✭ 53 (+211.76%)
Mutual labels:  promise
etsy-ts
Etsy API wrapper written in typescript
Stars: ✭ 18 (+5.88%)
Mutual labels:  promise
nancy
How JavaScript Promise Works
Stars: ✭ 26 (+52.94%)
Mutual labels:  promise
if any questions, please mail to [email protected]
Please install Node.js v4 or above from nodejs.org

How to avoid callback hell

	> git clone http://github.com/fedor/co_demo
	> cd co_demo
	> npm install

Data and execution flow in the examples:
 - input -->
       (a)sync1() -->
           delay(2000) -->
               (a)sync2() -->
                   (a)sync3() -->
                       output

1. Sync version.
	
	PROS:
	 - Safe
	 - Readable: no Callback Hell
	 - Centralised error handling
	CONS:
	 - Sync

2. Async.
	
	PROS:
	 - Async
	CONS:
	 - Unsafe: no guarantee to catch "throw new Error()"
	 - No centralised error handling
	 - Callback hell

3. Promises.

	"The Promise object is used for asynchronous computations.
	A Promise represents a value which may be available now, or in the future, or never."
	— https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise

	PROS:
	 - Async
	 - Centralised error handling
	 - Less callback hell
	CONS:
	 - mini-Callback Hell
	 - 2 ways to report errors (callback and throw)
	TODO:
	 - Remove error handling. What would happen?

4. Generators.

	"Generators are functions which can be exited and later re-entered.
	Their context (variable bindings) will be saved across re-entrances."
	— https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/function*

	This is a showcase of generators. Not actually an example.
	
	NOTE:
	 - "function*" defines "generator function" a function you can re-enter
	 - Generator function returns "generator", an object to control its execution
	 - .next().value
	 - yield cause value return from generator function

5. Promises + Generators #1

	Try to use generator to resolve Promises without callbacks
	
	NOTE:
	 - main()
	 - runner()

6. Promises + Generators #2

	Add correct error handling (rejected Promises)

	NOTE:
	 - .throw() will actually return result, similar to .next()

7. Promises + Generators #3

	Allow runner to accept generator function, not generator

	NOTE:
	 - runner(main()) --> run(main)

8. Promises + Generators: result

	Turn runner into a Promise fabric.
	All custom async functions implemented with runner

	PROS:
	 - Async
	 - Safe
	 - Centralised error handling
	 - No callback hell
	 - Single way to report an error
	NOTE:
	 - async2() error report was simplified with runner
	 - delay() still require manual promisification

9. co and bluebird

	co is a popular "runner" from TJ. It can "yield" Promises,
	Promises inside arrays or objects, generators, etc.

	bluebird provides logic for promisification.
	Promisification: callback-function --> promise-return-function

--------

Suggestions:

 - Promisify functions with callbacks, use bluebird.promisify() && bluebird.promisifyAll().
 - Use co and for flow-control.
 - Avoid callback-based handlers for async code e.g. use for-loop, not .forEach()
 - Return Promise from async functions, not traditional-callbacks.
 - Create Promise-based functions via co
 - Only use "return new Promise(function(resolve, reject) {...})" to manually promisify interfaces

--------

BONUS: async.each() example in co_async_each.js
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].