# Networks **Published by:** [meteosrds](https://paragraph.com/@cryptovuive/) **Published on:** 2022-12-14 **URL:** https://paragraph.com/@cryptovuive/networks ## Content bấm next qua 2 cái trên ảnh1. đáp án CALLBACK FUNCTIONS/** * Runs a callback function immediately * @param {function} callbackFunction */ function runCallback(callbackFunction) { callbackFunction(); } module.exports = runCallback; /** * Runs a callback function immediately * @param {function} callbackFunction */ function runCallback(callbackFunction) { setTimeout(callbackFunction, 1000); } module.exports = runCallback; 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; 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; function forEach(arr, callback) { for (let i = 0; i < arr.length; i++) { callback(arr[i], i); } } module.exports = forEach; 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 PROMISEScode 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; 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; function timer() { const promise = new Promise(function (resolve, reject) { setTimeout(function () { resolve('resolve'); }, 1000); }); return promise; } module.exports = timer; function timer() { const promise = new Promise(function (resolve, reject) { setTimeout(function () { resolve('resolve'); }, 1000); }); return promise; } module.exports = timer; 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; 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 LIBRARYcode 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; 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; 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; 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; 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; 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; 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; 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; vào xem và next qua 2 mục này là kết thúc phần network ## Publication Information - [meteosrds](https://paragraph.com/@cryptovuive/): Publication homepage - [All Posts](https://paragraph.com/@cryptovuive/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@cryptovuive): Subscribe to updates - [Twitter](https://twitter.com/meteosrds): Follow on Twitter