Alchemy University 学习教程,Networks(第三周)

估值102亿融资5.45亿的Alchemy 项目,大学每周任务逐步开始。持有大学生早期卡的伙伴们可以开始大学生活了:

官方大佬的 lens ,可以关注下:

第三周实际操作的只有一节内容:

Asynchronous JavaScript

Callback Functions

1: Run Callback

查看要求:

输入:

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

module.exports = runCallback;

点击运行。

2: Run Callback Async

查看要求:

输入:

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

module.exports = runCallback;

点击运行。

3: Dialog Callback

查看要求:

输入:

class Dialog {
    onClose(callbackFunction) {
        this.c = callbackFunction;
    }

    close() {
        this.c();
    }
}

module.exports = Dialog;

点击运行。

4: Dialog Callbacks

查看要求:

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;

点击运行。

5: For Each Callback

查看要求:

输入:

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

module.exports = forEach;

点击运行。

6: Map Callback

查看要求:

输入:

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

    return a;
}

module.exports = map;

点击运行。

Introduction to Promises

1: Request Order

查看要求:

输入:

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;

点击运行。

2: Catch Error

查看要求:

输入:

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;

点击运行。

3: Create Promise

查看要求:

function timer() {

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

    return promise;
}

module.exports = timer;

点击运行。

4: Asynchronous Timer

查看要求:

输入:

function timer() {

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

    return promise;
}

module.exports = timer;

点击运行。

5: Async Await

查看要求:

输入:

const { getResults } = require('./lab');
const { sendResults } = require('./messaging');
const { logResponse, logError } = require('./logs');

async function handleResults(patientId) {
      const res = await getResults(patientId);
      const r = await sendResults(patientId, res);
      await logResponse(r);
}

module.exports = handleResults;

点击运行。

6: Catch Await

查看要求:

输入:

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;

点击运行。

Pact: A Promise Library

1: Then and Catch

查看要求:

输入:

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

module.exports = Pact;

点击运行。

2: Executor Function

查看要求:

输入:

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

module.exports = Pact;

点击运行。

3: Then... What?

查看要求:

输入:

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

module.exports = Pact;

点击运行。

4: Catch the Rejection

查看要求:

输入:

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

module.exports = Pact;

点击运行。

5: Multiple Callbacks

查看要求:

输入:

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

module.exports = Pact;

点击运行。

6: Immediate Resolve

查看要求:

输入:

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.thens[i](value);
            }
        };
        this.f2 = (value)=> {
            this.rejectedValue = value;
            this.status = STATUS.REJECTED;
            for (let i = 0; i < this.catchs.length; i++) {
                this.catchs[i](value);
            }
        };
        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;

点击运行。

7: Chaining Callbacks

查看要求:

输入:

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.thens[i](value);
            }
        };
        this.f2 = (value)=> {
            this.rejectedValue = value;
            this.status = STATUS.REJECTED;
            for (let i = 0; i < this.catchs.length; i++) {
                this.catchs[i](value);
            }
        };
        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;

点击运行。

8: Chaining Promises

查看要求:

输入:

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.thens[i](value);
            }
        };
        this.f2 = (value)=> {
            this.rejectedValue = value;
            this.status = STATUS.REJECTED;
            for (let i = 0; i < this.catchFn.length; i++) {
                this.catchs[i](value);
            }
        };
        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://twitter.com/bitc2024

Subscribe to andrecronje
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.