Cover photo

Networks

post image

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

1. đáp án CALLBACK FUNCTIONS

post image
post image
/**
 * Runs a callback function immediately
 * @param {function} callbackFunction
 */
function runCallback(callbackFunction) {
    callbackFunction();
}

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

module.exports = runCallback;
post image
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;
post image
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;
post image
function forEach(arr, callback) {
    for (let i = 0; i < arr.length; i++) {
        callback(arr[i], i);
    }
}

module.exports = forEach;
post image
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

post image
post image
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;
post image
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;
post image
function timer() {

    const promise = new Promise(function (resolve, reject) {
        setTimeout(function () {
            resolve('resolve');
        }, 1000);
    });

    return promise;
}

module.exports = timer;
post image
function timer() {

    const promise = new Promise(function (resolve, reject) {
        setTimeout(function () {
            resolve('resolve');
        }, 1000);
    });

    return promise;
}

module.exports = timer;
post image
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;
post image
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

post image
post image
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;
post image
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;
post image
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;
post image
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;
post image
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;
post image
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;
post image
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;
post image
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;
post image

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