https://github.com/uppercod/next-step
👟 Create an iterator similar to the return of a generating function, with which you can execute a list of functions next to complete it.
https://github.com/uppercod/next-step
Last synced: about 2 months ago
JSON representation
👟 Create an iterator similar to the return of a generating function, with which you can execute a list of functions next to complete it.
- Host: GitHub
- URL: https://github.com/uppercod/next-step
- Owner: UpperCod
- License: apache-2.0
- Created: 2018-04-19T23:38:36.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-04-21T20:40:42.000Z (about 8 years ago)
- Last Synced: 2025-01-24T10:48:10.430Z (over 1 year ago)
- Language: JavaScript
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# next-step
Create an iterator similar to the return of a generating function, with which you can execute a list of functions next to complete it.
## Generators
```js
function *sample(){
yield 1;
yield 2;
return 3;
}
let iterator = sample();
iterator.next() // {value : 1 , done : false}
iterator.next() // {value : 2 , done : false}
iterator.next() // {value : 3 , done : false}
iterator.next() // {value : undefined , done : true}
```
## next-step
```js
import step from "next-step";
function sample(){
return step(
()=>1,
()=>2,
()=>3
);
}
let iterator = sample();
iterator.next() // {value : 1 , done : false}
iterator.next() // {value : 2 , done : false}
iterator.next() // {value : 3 , done : false}
iterator.next() // {value : undefined , done : true}
```
## Motivation
Generators are the best advance for javascript, but its compatibility in browsers is not yet complete,
I personally occupy them in my library [Kubox] (https://www.github.com/uppercod/kubox), for asynchronous handling in the change of state.
### Ejemplo con kubox
```js
export default function *api(state,value){
yield {loading : true};
yield fetch("//sample.api")
.then((response)=>response.json())
.then((response)=>{loading:false,response})
}
```
> The `Kubox.recycle` method inside **kubox**, allows you to recycle the return of a generator by modifying the state as you iterate over it with the use of the **next** method, if` Kubox.recycle` You receive a promise, you wait until you run again **next**. I hope that the generators are a few RockStar 🤟 coming soon, much more than the asynchronous functions.
### Example with kubox + next-step
```js
import step from "next-step";
export default function api(state,value){
return step(
()=>({loading:true}),
()=>fetch("//sample.api")
.then((response)=>response.json())
.then((response)=>{loading:false,response})
)
}
```
> As you will notice there is not much difference, it is just a little less elegant, but it works in all browsers 🛠️.