# block-run

> block run code

Latest version **1.0.10** (published 2018-04-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install block-run
pnpm add block-run
yarn add block-run
bun add block-run
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.10 |
| Published | 2018-04-17 |
| First published | 2018-04-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 11.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | zy445566 |
| Maintainers | zy445566 |
| Keywords | block-run, block, run, code |

## Links

- npm: https://www.npmjs.com/package/block-run
- Repository: https://github.com/zy445566/block-run
- Homepage: https://github.com/zy445566/block-run#readme
- Issues: https://github.com/zy445566/block-run/issues
- npm.io page: https://npm.io/package/block-run

## Dependencies (1)

- [co](https://npm.io/package/co.md) ^4.6.0

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 1.0.10 (latest) — 2018-04-17
- 1.0.9 — 2018-04-15
- 1.0.8 — 2018-04-15
- 1.0.7 — 2018-04-15
- 1.0.6 — 2018-04-15
- 1.0.5 — 2018-04-14
- 1.0.4 — 2018-04-14
- 1.0.3 — 2018-04-14
- 1.0.2 — 2018-04-12
- 1.0.1 — 2018-04-12
- 1.0.0 — 2018-04-12

## README

# block-run
block run code
Execute code block in sequence by channels

# install
```sh
npm install block-run
```

# api
### run
-------------------------
* FunctionName  run
    * Return  anything
    * Description  run block code in channel
    * Param

name        | type |require |default    |Description
------------|------|--------|-----------|------------
channel   |string|must  |null   |block code channel
func |Function or GeneratorFunction or AsyncFunction|must  |null          |block code function
funTimeout |number|option  |-1         |function timeout 

### getQueue
-------------------------
* FunctionName  getQueue
    * Return  object
    * Description  get all channel queue
    * Param nothing

# base example
```js
const assert = require('assert');
const co = require('co');
const BlockRun = require('block-run');

//Promise 
BlockRun.run('testChannel1',()=>{
    return 1;
}).then((res)=>{
    assert.equal(res,1,'Promise failed');
}).catch((e)=>{
    throw e;
});


let p1 =(index,time)=>{
    return new Promise((res,rej)=>{
        setTimeout(()=>{
            res(index);
        },time)
    });
}

// testChannel2 make two async function in sequence 

//co yield 
co(function*(){
    try{
        for(let i = 0;i<10;i++)
        {
            let res = yield BlockRun.run('testChannel2',function* (){
                return yield p1(i,2000);
            });
            assert.equal(res,i,'co yield failed'+res);
        }
    } catch(e)
    {
        throw e;
    }
});

//async
(async () =>{
    try{
        for(let i = 0;i<10;i++)
        {
            let res = await BlockRun.run('testChannel3',async ()=>{
                return await p1(i,2000);
            });
            assert.equal(res,i,'async failed'+res);
        }
    } catch(e)
    {
        throw e;
    }
})();
```

# different
## before
### code:
```js
let p1 =(index,time)=>{
   return new Promise((res,rej)=>{
       setTimeout(()=>{
           res(index);
       },time)
   });
}

(async () =>{
       for(let i = 0;i<10;i++)
       {
            console.log(await p1(i,1000))
       }
})();
(async () =>{
       for(let i = 0;i<10;i++)
       {
            console.log(await p1(i,1000))
       }
})();
```
### result:
```
one seconds two number 
two async function run at the same time
```

## aftar
### code:
```js
const BlockRun = require('block-run');
let p1 =(index,time)=>{
   return new Promise((res,rej)=>{
       setTimeout(()=>{
           res(index);
       },time)
   });
}

(async () =>{
       for(let i = 0;i<10;i++)
       {
           let res = await BlockRun.run('sameChannel',async ()=>{
               return await p1(i,1000);
           });
		console.log(res)
       }
})();
(async () =>{
       for(let i = 0;i<10;i++)
       {
           let res = await BlockRun.run('sameChannel',async ()=>{
               return await p1(i,1000);
           });
		console.log(res)
       }
})();
```
### result:
```
one seconds one number 
two async function run in sequence by same channel name
```


# other 
[针对使用非块运行和块运行并发压测对比](https://github.com/zy445566/myBlog/tree/master/20180414block-run)

---
_Source: https://npm.io/package/block-run · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
