st-async

1.1.0 • Public • Published

st-async

Be lazy, Write short

/*
    Install by below
    $ npm install st-async
*/

let stAsync = require('st-async');
stAsync.set_promise(false); // give true as using Example 5, 6, 7, 8

// Example 1
stAsync(
    o => { o('Hello'); },
    o => { o('World'); },
    stAsync.finally(list => {
        console.log(list); // ["Hello","World"]
    }),
)

// Example 2
stAsync(
    o => {
        o('hello');
    },
    o => {
        console.log(o.data); // hello
        o('world');
    },
    o => {
        console.log(o.data); // world
        o();
    }
)

// Example 3
stAsync(
    o => {
        o('hello');
    },
    o => {
        console.log(o.data); // hello
        o('world');
    },
    o => {
        console.log(o.data); // world
        o(null, 'error');
    },
    o => {
        console.log(o.data); // Can't run
        o();
    },
    {
        catch: o => {
            console.log(o); // error
        },
        finally: o => {
            console.log(o); // ['hello', 'world']
        },
    }
)

// Example 4
stAsync(
    o => {
        o('Banana', 'Errrror!');
    },
    o => {
        console.log(o.data); // Can't run
        o('Fish');
    },
    stAsync.catch(error => {
        console.log(error); // Errrror!
    }),
    stAsync.finally(list => {
        console.log(JSON.stringify(list)); // undefined
    }),
)

// Example 5
stAsync(
    data => new Promise((resolve, reject) => {
        resolve('banana');
    }),
    data => new Promise((resolve, reject) => {
        console.log(data); // banana
        resolve('mango');
    }),
    (data, resolve) => {
        console.log(data); // mango
        resolve('kiwi');
    },
    (data, resolve, reject) => {
        console.log(data); // kiwi
        reject('lemon');
    },
    data => new Promise((resolve, reject) => {
        console.log(data);
        reject('water');
    }),
    stAsync.catch(reject_msg => {
        console.log(reject_msg); // lemon
    }),
    stAsync.finally(list => {
        console.log(list); // [ 'banana', 'mango', 'kiwi' ]
    })
)

// Example 6
stAsync(
    data => stAsync(
        data => new Promise((resolve, reject) => {
            resolve('head');
        }),
        data => new Promise((resolve, reject) => {
            console.log(data); // head
            resolve('first');
        }),
        stAsync.finally(list => {
            console.log(list); // [ 'head', 'first' ]
        })
    ),
    data => new Promise((resolve, reject) => {
        resolve('banana');
    }),
    data => new Promise((resolve, reject) => {
        console.log(data); // banana
        resolve('mango');
    }),
    (data, resolve) => {
        console.log(data); // mango
        resolve('kiwi');
    },
    (data, resolve, reject) => {
        console.log(data); // kiwi
        reject('lemon');
    },
    data => new Promise((resolve, reject) => {
        console.log(data);
        reject('water');
    }),
    stAsync.catch(reject_msg => {
        console.log(reject_msg); // lemon
    }),
    stAsync.finally(list => {
        console.log(list); // [ [ 'head', 'first' ], 'banana', 'mango', 'kiwi' ]
    })
)


// Example 7
stAsync(
    data => fetch('http://dummy.restapiexample.com/api/v1/employees').then(res => res.json()),
    data => fetch('http://dummy.restapiexample.com/api/v1/employees').then(res => res.json()),
    data => fetch('http://dummy.restapiexample.com/api/v1/employees').then(res => res.json()),
    data => fetch('http://dummy.restapiexample.com/api/v1/employees').then(res => res.json()),
    stAsync.finally(list => {
        console.log(list); // All resolved responses until now are in list
    })
)

// Example 8
stAsync(
    data => fetch('http://dummy.restapiexample.com/api/v1/employees').then(res => res.json()),
    data => fetch('http://dummy.restapiexample.com/api/v1/create', {
        method: 'POST', // or 'PUT'
        body: JSON.stringify({ "name": "test", "salary": "123", "age": "23" }),
        headers: { 'Content-Type': 'application/json' }
    }).then(res => res.json()),
    (data, resolve, reject) => {
        console.log(data); // response of http://dummy.restapiexample.com/api/v1/employees
        resolve();
    },
    stAsync.finally(list => {
        console.log(list); // All resolved responses until now are in list
    })
)

Readme

Keywords

none

Package Sidebar

Install

npm i st-async

Weekly Downloads

0

Version

1.1.0

License

ISC

Unpacked Size

9.9 kB

Total Files

3

Last publish

Collaborators

  • kstost