nv-perform-resume

1.0.1 • Public • Published

nv-perform-resume

  • nv-perform-resume is very simple util to let you use perform/resume style for error-handling
  • can only be used in async-function
  • your runtime need to support Event and EventTarget
  • it provide 3 functions: frame,perform,resume

install

  • npm install nv-perform-resume

usage

  const {frame,perform,resume} = require("nv-perform-resume");

examples

    function creat_user(name) {
        name = name?name:null
        let d = {name:name}
        d.friends = new Set()
        return(d)
    }
    
    var arya = creat_user();           //arya forget to set name when init
    var gendry = creat_user("gendry");
    
    /*
    > arya
    { name: null, friends: Set(0) {} }
    > gendry
    { name: 'gendry', friends: Set(0) {} }
    >
    */
    
    async function get_name(user) {
        let name = user.name;
        if (name === null) {
            //=============>perform similiar to Throw new Error()
            //=============>but recoverable
            console.log("wait error handling")

            let recoverable = perform("null-error");    //just like Raise/Throw Error
            name = await recoverable;                   //[&A]
            
            console.log("error recovered!!")
        }
        return(name)
    }
    
    
    async function make_friends(user1, user2) {
        let f = frame(
            get_name,                                //====>the original function,used as function-template here
            err => {                             
                console.log("error type: ",err)
                if(err==="null-error"){                      //resume the "null-error" with a good-value "arya"
                    //resume similiar to try {} catch {}
                    //but is used to recover the excution
                    resume("arya")                          //====================>resume the function at [&A]
                }
            }
        );
        user1.friends.add(await f(user2));
        user2.friends.add(await f(user1));
    }
    
    (async ()=> {await make_friends(arya, gendry);})();
    
    
    /*
    > arya
    { name: null, friends: Set(1) { 'gendry' } }
    > gendry
    { name: 'gendry', friends: Set(1) { 'arya' } }
    >
    */

THANKS TO

LICENSE

  • ISC

Package Sidebar

Install

npm i nv-perform-resume

Weekly Downloads

3

Version

1.0.1

License

ISC

Unpacked Size

5.86 kB

Total Files

6

Last publish

Collaborators

  • ihgazni2