New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

bonnet

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bonnet

Bonnet is a simple tool for distribute your long running blocking tasks in time, using ES6 generators.

latest
Source
npmnpm
Version
0.0.4
Version published
Maintainers
1
Created
Source

bonnet

Bonnet is a simple tool for distributing your long running blocking tasks in time using ES6 generators in node.

Using Bonnet you are able to split your computation into pieces with yield and in the end return your result. Bonnet returns a promise and resolves it, if your function finished or rejects it, if you throw an error. The benefit of this is that you won't block your event loop if you are working on small fast parts.

Installation

$ npm install bonnet

And after:

var bonnet = require('bonnet');
bonnet(myGenerator);

How to use

Simple example

var bonnet = require('bonnet');
bonnet(function* () {
     var sum = 0;
     for (var i = 0; i <= 10000; i++) {
            yield sum = sum + i;
     }
     return sum;
}).then(function (data) {
    console.log(data);
}, function (error) {
    console.log(error);
});

Combine with promises

var bonnet = require('bonnet');
var Promise = require('promise');


function myAsynchronousTask() {
    return new Promise(function (resolve, reject) {
        setTimeout(function () {
            resolve("bar");
        }, 10);
    });
}

bonnet(function* () {
    var result = "foo";
    result += yield myAsynchronousTask();
    result += yield "foo";
    return result;
}).then(function (data) {
    console.log(data); //foobarfoo
});

Error handling

var bonnet = require('bonnet');
bonnet(function* () {
    var somethingWrong = true;
    if(somethingWrong){
       throw new Error("ERROR");
    }  
}).then(null, function (error) {
    console.log(error);
});

Wrapper

var wrap = require('bonnet').wrap;
var wrappedFunction = wrap(function* (a,b) {
      var str = "";
      for (var i=0; i<=10000; i++){
        str =  yield (new Array(5)).join(b);
      }
      return a+str;
});

wrappedFunction("foo","bar").then(function(data){
    console.log(data);  //foobarbarbarbar
});

Test

  $ npm test

Keywords

async

FAQs

Package last updated on 31 Jan 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts