https://github.com/ahabhgk/promise-node
A simple way to handle asynchronous.
https://github.com/ahabhgk/promise-node
Last synced: 2 months ago
JSON representation
A simple way to handle asynchronous.
- Host: GitHub
- URL: https://github.com/ahabhgk/promise-node
- Owner: ahabhgk
- License: mit
- Created: 2019-07-17T08:06:25.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T04:39:23.000Z (over 2 years ago)
- Last Synced: 2025-01-20T08:16:18.345Z (5 months ago)
- Language: JavaScript
- Size: 918 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# promise-node
A simple way to handle asynchronous.
## install
```shell
$ yarn add promise-node
```or
```shell
$ npm install --save promise-node
```## usage
Use `.start(param1, param2, ...)` to pass arguments to the first promiseNode's function.
Use `.next(param1, param2, ...)` to pass arguments to next promiseNode's function.
When you using `function () {}`, you can use `this.next()` or `instance.next()` to resolve this function, but when you using arrow function, `instance.next()` is the only way to resolve this arrow function.
### `class` mode
```js
import PromiseNode from 'promise-node'const p1 = new PromiseNode(function (a) {
console.log('start')
setTimeout(() => {
console.log(a++)
this.next(a, 'hah')
}, 2000)
})
const p2 = new PromiseNode((b, str) => {
console.log(b, str)
p2.next(b + str)
})
const p3 = new PromiseNode((c) => {
setTimeout(() => {
console.log(c)
console.log('end')
}, 1000)
})p1.setNext(p2).setNext(p3)
p1.start(0)
```### `fn` mode
When you open this mode, the `Function.prototype` will add two function.
```js
import PromiseNode from 'promise-node'PromiseNode.fn() // open `fn` mode
const p1 = function (a) {
console.log('start')
setTimeout(() => {
console.log(a++)
p1.next(a, 'hah')
}, 2000)
}
const p2 = (b, str) => {
console.log(b, str)
p2.next(b + str)
}
const p3 = (c) => {
setTimeout(() => {
console.log(c)
console.log('end')
}, 1000)
}p1.setNext(p2).setNext(p3)
p1(0)
```## I made this repo just for fun and learn CoR pattern.
There are lots of better way to handle asynchronous, you even can change Koa's onion model (or other framework) to handle asynchronous, I'm just a FE beginner and very interested in it.