1.0.6 • Published 4 years ago

@idogo/i-co v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

i-co

Simple asynchronous working lib base on generator function.

Install

npm install @idogo/ico --save

Usage

import ico from '@idogo/ico';

yield a thunk.

function thunk(args) {
  return function(cb) {
    process.on('async task', function(err, data) {
      if(err) return cb(err, null);
      cb(null, data);
    });
  }
}
ico(function *() {
  const response1 = yield thunk();
  const response2 = yield thunk();
  return response1 + response1;
})(function(err, data) {
  /**
  * data is return from ico argument which is generator function
  * data === response1 + response2
  * */
  console.log(data);
});

yield a value(should package with a thunk first).

function thunk(value) {
  return function(cb) {
    cb(null, value);
  }
}

ico(function *() {
  const response1 = yield thunk(100);
  const response2 = yield thunk(200);
  return response1 + response1;
})(function(err, data) {
  console.log(data); // 300
});

yield a promise obj.

ico(function *() {
  const response1 = yield Promise.resolve(100);
  const response2 = yield Promise.resolve(200);
  const response3 = yield new Promise(resolve => {
    resolve(300);
  });
  return response1 + response1 + response3;
})(function(err, data) {
  console.log(data); // 600
});

yield a Array or Object

const array = [thunk(100), thunk(200), thunk(300)];
const obj = {
  t1: thunk(100),
  t2: thunk(200),
  t3: thunk(300),
  t4: {
    ['t4-1']: thunk(401),
    ['t4-2']: thunk(402)
  }
};
ico(function *() {
  const response1 = yield array;
  const response2 = yield obj;
  return [response1, response2];
})(function(err, data) {
  /**[[100, 200, 300], { t1: 100, t2: 200, t3: 300, t4: { 't4-1': 401, '4-2 : 402} }]*/
  console.log(data);
});

yield a generatorfunc or generator

ico(function *() {
  const response1 = yield function *(){
    const response1 = yield thunk(100);
    const response2 = yield thunk(200);
    return response1 + response2;
  };
  const response2 = yield (function *() {
    const response1 = yield thunk(300);
    const response2 = yield thunk(400);
    return response1 + response2;
  })();
})(function(err, data) {
  console.log(data); // 1000
});

Test

npm run test
npm run test:watch
1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago