# Networks

By [meteosrds](https://paragraph.com/@cryptovuive) · 2022-12-14

---

![](https://storage.googleapis.com/papyrus_images/36dc2484ba81e8e2bc2211c6716dff03233c63d1183ce216efc83866bf9c53eb.png)

bấm next qua 2 cái trên ảnh

1\. đáp án CALLBACK FUNCTIONS
-----------------------------

![](https://storage.googleapis.com/papyrus_images/56c609794c5d3cf2e4cc85427b3cdd4c1f76ec44c9b3ad83487776020667c206.png)

![](https://storage.googleapis.com/papyrus_images/d64c0142ad571dd9a5aabc071299b1cda73ced70f3b7ffcafd8c63ff135aa689.png)

    /**
     * Runs a callback function immediately
     * @param {function} callbackFunction
     */
    function runCallback(callbackFunction) {
        callbackFunction();
    }
    
    module.exports = runCallback;
    

![](https://storage.googleapis.com/papyrus_images/54910ec5eae864f7a24d312642193390660f0c18f84d5a41bb455190a822de42.png)

    /**
     * Runs a callback function immediately
     * @param {function} callbackFunction
     */
    function runCallback(callbackFunction) {
        setTimeout(callbackFunction, 1000);
    }
    
    module.exports = runCallback;
    

![](https://storage.googleapis.com/papyrus_images/2fbf5f3f633b973ffcfd0dfde6e61f2cf26a725779ac1d91810192fc3c24ab41.png)

    class Dialog {
        onClose(callbackFunction) {
            this.carr = this.carr || [];
            this.carr.push(callbackFunction);
        }
    
        close() {
            for (let i = 0; i < this.carr.length; i++) {
                this.carr[i]();
            }
        }
    }
    
    module.exports = Dialog;
    

![](https://storage.googleapis.com/papyrus_images/630de547c3561f60186c65012fca357c76df1cebc8842991a2c9ec9d02f78c6f.png)

    class Dialog {
        onClose(callbackFunction) {
            this.carr = this.carr || [];
            this.carr.push(callbackFunction);
        }
    
        close() {
            for (let i = 0; i < this.carr.length; i++) {
                this.carr[i]();
            }
        }
    }
    
    module.exports = Dialog;
    

![](https://storage.googleapis.com/papyrus_images/ddbd2b80c370b8d1825f44db1714dcec69dba36942bb6e91ded7fb2b8701818b.png)

    function forEach(arr, callback) {
        for (let i = 0; i < arr.length; i++) {
            callback(arr[i], i);
        }
    }
    
    module.exports = forEach;
    

![](https://storage.googleapis.com/papyrus_images/d0605d1023ee9b02672fc33d0ce14f5800942c3a87395474c943f705fb147b6c.png)

    function map(arr, callback) {
        let a = [];
        for (let i = 0; i < arr.length; i++) {
            a.push(callback(arr[i]));
        }
    
        return a;
    }
    
    module.exports = map;
    

2\. đáp án INTRODUCTION TO PROMISES
-----------------------------------

![](https://storage.googleapis.com/papyrus_images/8f823b6eaadf9b1b485246f3b8807e310216777ba46c39db195377eba04d12fe.png)

![](https://storage.googleapis.com/papyrus_images/e01708e568513b229c185c54702cc5aa84672d691e662d3ee6aefcaf50604643.png)

    code bên dưới
    

    const { makeFood } = require('./Kitchen');
    
    class Order {
        constructor() {
            this.isReady = false;
        }
        request(food) {
            const promise = makeFood(food);
            promise.then( (food) => {
                this.isReady = true;
            });
        }
    }
    
    module.exports = Order;
    

![](https://storage.googleapis.com/papyrus_images/173edfd5cd0da925910336b1c24e725ceafa581e9a932b7769803b678a9a6f47.png)

    const { makeFood } = require('./Kitchen');
    
    class Order {
        constructor() {
            this.isReady = false;
        }
        request(food) {
            const promise = makeFood(food);
            promise.then((food) => {
                this.isReady = true;
            });
            promise.catch((err) => {
                this.error = err;
            });
        }
    }
    
    module.exports = Order;
    

![](https://storage.googleapis.com/papyrus_images/70d7b95b9a4a3683d9be21e767c07ff52b2ef513c25a2601fa6a6ffbf67924cb.png)

    function timer() {
    
        const promise = new Promise(function (resolve, reject) {
            setTimeout(function () {
                resolve('resolve');
            }, 1000);
        });
    
        return promise;
    }
    
    module.exports = timer;
    

![](https://storage.googleapis.com/papyrus_images/7c1621359c75669ff6b7651500fb4428d52bd959e882346c2165c3787663e5d4.png)

    function timer() {
    
        const promise = new Promise(function (resolve, reject) {
            setTimeout(function () {
                resolve('resolve');
            }, 1000);
        });
    
        return promise;
    }
    
    module.exports = timer;
    

![](https://storage.googleapis.com/papyrus_images/474136fb5e369d88e302ca68f3b950f6851b0ca07006a64b8849e02f4514e918.png)

    const { getResults } = require('./lab');
    const { sendResults } = require('./messaging');
    const { logResponse, logError } = require('./logs');
    
    async function handleResults(patientId) {
        try {
            const res = await getResults(patientId);
            const r = await sendResults(patientId, res);
            await logResponse(r);
        } catch (ex) {
            logError(ex);
        }
    }
    
    module.exports = handleResults;
    

![](https://storage.googleapis.com/papyrus_images/d7cd52dbac36fa663e15a29f1f9f8bf339ec961f0b29e84cf09910414d9cf398.png)

    const { getResults } = require('./lab');
    const { sendResults } = require('./messaging');
    const { logResponse, logError } = require('./logs');
    
    async function handleResults(patientId) {
        try {
            const res = await getResults(patientId);
            const r = await sendResults(patientId, res);
            await logResponse(r);
        } catch (ex) {
            logError(ex);
        }
    }
    
    module.exports = handleResults;
    

3\. đáp án PACT: A PROMISE LIBRARY
----------------------------------

![](https://storage.googleapis.com/papyrus_images/b18811122d8bd36fe2e7721361f99fa7c52f7b4b6de78ec0a5a1309a1733f158.png)

![](https://storage.googleapis.com/papyrus_images/e52e607c5e2cb2b6e1f0ce3fceddac351b000e726a8d27b5650e6706b06feb1c.png)

    code bên đưới 
    

    class Pact {
        constructor(fn) {
            this.catchs = [];
            this.thens = [];
            this.f1 = (value)=> {
                for (let i = 0; i < this.thens.length; i++) {
                    this.thensi;
                }
            };
            this.f2 = (value)=> {
                for (let i = 0; i < this.catchs.length; i++) {
                    this.catchsi;
                }
            };
            fn(this.f1, this.f2);
        }
        catch(c) {
            this.catchs.push(c);
        }
        then(t) {
            this.thens.push(t);
        }
    }
    
    module.exports = Pact;
    

![](https://storage.googleapis.com/papyrus_images/847bb312d32bdd1c3956cf565d4e98af1407114bb9e9d9200db1686f19dc4e2a.png)

    code bên dưới 
    

    class Pact {
        constructor(fn) {
            this.catchs = [];
            this.thens = [];
            this.f1 = (value)=> {
                for (let i = 0; i < this.thens.length; i++) {
                    this.thensi;
                }
            };
            this.f2 = (value)=> {
                for (let i = 0; i < this.catchs.length; i++) {
                    this.catchsi;
                }
            };
            fn(this.f1, this.f2);
        }
        catch(c) {
            this.catchs.push(c);
        }
        then(t) {
            this.thens.push(t);
        }
    }
    
    module.exports = Pact;
    

![](https://storage.googleapis.com/papyrus_images/1f2ddda4c7d46500f653d2f29ab290bd0ca15507e9c51d79a4de837a8f340d6d.png)

    code bên dưới
    

    class Pact {
        constructor(fn) {
            this.catchs = [];
            this.thens = [];
            this.f1 = (value)=> {
                for (let i = 0; i < this.thens.length; i++) {
                    this.thensi;
                }
            };
            this.f2 = (value)=> {
                for (let i = 0; i < this.catchs.length; i++) {
                    this.catchsi;
                }
            };
            fn(this.f1, this.f2);
        }
        catch(c) {
            this.catchs.push(c);
        }
        then(t) {
            this.thens.push(t);
        }
    }
    
    module.exports = Pact;
    

![](https://storage.googleapis.com/papyrus_images/c1043e1f398f8e9b8e06d3f1e9d69e94358fbc8c3f94f06505981e472975f012.png)

    code bên dưới
    

    class Pact {
        constructor(fn) {
            this.catchs = [];
            this.thens = [];
            this.f1 = (value)=> {
                for (let i = 0; i < this.thens.length; i++) {
                    this.thensi;
                }
            };
            this.f2 = (value)=> {
                for (let i = 0; i < this.catchs.length; i++) {
                    this.catchsi;
                }
            };
            fn(this.f1, this.f2);
        }
        catch(c) {
            this.catchs.push(c);
        }
        then(t) {
            this.thens.push(t);
        }
    }
    
    module.exports = Pact;
    

![](https://storage.googleapis.com/papyrus_images/dfbea46bc5576e21b4b34907ce3a91a6d8cb209bdd954d33843505715e831a9d.png)

    code bên dưới 
    

    class Pact {
        constructor(fn) {
            this.catchs = [];
            this.thens = [];
            this.f1 = (value)=> {
                for (let i = 0; i < this.thens.length; i++) {
                    this.thensi;
                }
            };
            this.f2 = (value)=> {
                for (let i = 0; i < this.catchs.length; i++) {
                    this.catchsi;
                }
            };
            fn(this.f1, this.f2);
        }
        catch(c) {
            this.catchs.push(c);
        }
        then(t) {
            this.thens.push(t);
        }
    }
    
    module.exports = Pact;
    

![](https://storage.googleapis.com/papyrus_images/f9f5af45d066e681ef6078136c3ea812d041b3c8a3e52055aa128a70d043d70b.png)

    code bên dưới 
    

    const STATUS = {
        PENDING: 0,
        RESOLVED: 1,
        REJECTED: 2,
    }
    
    class Pact {
        constructor(fn) {
            this.catchs = [];
            this.thens = [];
            this.status = STATUS.PENDING;
            this.f1 = (value)=> {
                this.resolvedValue = value;
                this.status = STATUS.RESOLVED;
                for (let i = 0; i < this.thens.length; i++) {
                    this.thensi;
                }
            };
            this.f2 = (value)=> {
                this.rejectedValue = value;
                this.status = STATUS.REJECTED;
                for (let i = 0; i < this.catchs.length; i++) {
                    this.catchsi;
                }
            };
            fn(this.f1, this.f2);
        }
        catch(c) {
            if (this.status === STATUS.PENDING) {
                this.catchs.push(c);
            }
            else if (this.status === STATUS.REJECTED) {
                c(this.rejectedValue);
            }
        }
        then(t) {
            if (this.status === STATUS.PENDING) {
                this.thens.push(t);
            }
            else if (this.status === STATUS.RESOLVED) {
                t(this.resolvedValue);
            }
        }
    }
    
    module.exports = Pact;
    

![](https://storage.googleapis.com/papyrus_images/1dac73ab720740fad92548b22c3093540474c0ef29192fe529f38fbea2726302.png)

    code bên dưới 
    

    const STATUS = {
        PENDING: 0,
        RESOLVED: 1,
        REJECTED: 2,
    }
    
    class Pact {
        constructor(fn) {
            this.catchs = [];
            this.thens = [];
            this.status = STATUS.PENDING;
            this.f1 = (value)=> {
                this.resolvedValue = value;
                this.status = STATUS.RESOLVED;
                for (let i = 0; i < this.thens.length; i++) {
                    this.thensi;
                }
            };
            this.f2 = (value)=> {
                this.rejectedValue = value;
                this.status = STATUS.REJECTED;
                for (let i = 0; i < this.catchs.length; i++) {
                    this.catchsi;
                }
            };
            fn(this.f1, this.f2);
        }
        catch(c) {
            if (this.status === STATUS.PENDING) {
                this.catchs.push(c);
            }
            else if (this.status === STATUS.REJECTED) {
                c(this.rejectedValue);
            }
        }
        then(t) {
            if (this.status === STATUS.PENDING) {
                return new Pact((resolve, reject) => {
                    this.thens.push((val) => {
                        if (val instanceof Pact) {
                            val.then((val) => resolve(t(val)));
                        }
                        else {
                            resolve(t(val));
                        }
                    });
                });
            }
            else if (this.status === STATUS.RESOLVED) {
                t(this.resolvedValue);
            }
        }
    }
    
    module.exports = Pact;
    

![](https://storage.googleapis.com/papyrus_images/4c22cab04301761c0d0747916ab42b5095fe794faf5d0fbb2ffa68c39f1b2cb1.png)

    code bên dưới 
    

    const STATUS = {
        PENDING: 0,
        RESOLVED: 1,
        REJECTED: 2,
    }
    
    class Pact {
        constructor(fn) {
            this.catchs = [];
            this.thens = [];
            this.status = STATUS.PENDING;
            this.f1 = (value)=> {
                this.resolvedValue = value;
                this.status = STATUS.RESOLVED;
                for (let i = 0; i < this.thens.length; i++) {
                    this.thensi;
                }
            };
            this.f2 = (value)=> {
                this.rejectedValue = value;
                this.status = STATUS.REJECTED;
                for (let i = 0; i < this.catchFn.length; i++) {
                    this.catchsi;
                }
            };
            fn(this.f1, this.f2);
        }
        catch(c) {
            if (this.status === STATUS.PENDING) {
                this.catchs.push(c);
            }
            else if (this.status === STATUS.REJECTED) {
                c(this.rejectedValue);
            }
        }
        then(t) {
            if (this.status === STATUS.PENDING) {
                return new Pact((resolve, reject) => {
                    this.thens.push((val) => {
                        if (val instanceof Pact) {
                            val.then((val) => resolve(t(val)));
                        }
                        else {
                            resolve(t(val));
                        }
                    });
                });
            }
            else if (this.status === STATUS.RESOLVED) {
                t(this.resolvedValue);
            }
        }
    }
    
    module.exports = Pact;
    

![](https://storage.googleapis.com/papyrus_images/b3ce78b601b4644cd7a950217bd497e8b30d2646e600ba835c2f64b825a1219e.png)

**vào xem và next qua 2 mục này là kết thúc phần network**

---

*Originally published on [meteosrds](https://paragraph.com/@cryptovuive/networks)*
