Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为Browser和node.js编写公共类_Javascript_Node.js - Fatal编程技术网

Javascript 为Browser和node.js编写公共类

Javascript 为Browser和node.js编写公共类,javascript,node.js,Javascript,Node.js,我已经为承诺编写了一个包装器来处理多个承诺,并在浏览器窗口中漂亮地呈现它们。我通过创建3个类实现了这一点,分别是Process、ProcessQueue和ProcessStatus 现在我想将DOM操作与代码分离,并将Promise处理模块也移植到node.js 我看到了一个关于这个例子的例子。但我不确定如何在课堂上做到这一点 我删除了DOM操作代码并添加了导入语句 Process.js import { ProcessStatus } from './ProcessStatus'; clas

我已经为承诺编写了一个包装器来处理多个承诺,并在浏览器窗口中漂亮地呈现它们。我通过创建3个类实现了这一点,分别是
Process
ProcessQueue
ProcessStatus

现在我想将DOM操作与代码分离,并将Promise处理模块也移植到node.js

我看到了一个关于这个例子的例子。但我不确定如何在课堂上做到这一点

我删除了DOM操作代码并添加了导入语句

Process.js

import { ProcessStatus } from './ProcessStatus';

class Process {
    constructor(invoker) {
        if (invoker instanceof Function) {
            this._promise = new Promise(invoker);
        } else if (invoker instanceof Promise) {
            this._promise = invoker;
        } else {
            throw new TypeError('Invalid invoker passed for Process constructor');
        }

        this._status = ProcessStatus.INPROGRESS;
        this._promise.then(function () {
            this._status = ProcessStatus.SUCCESS;
        }.bind(this), function () {
            this._status = ProcessStatus.FAILED;
        }.bind(this));

    }

    then(resolve, reject) {
        return this._promise.then(resolve, reject);
    }

    catch(reject) {
        return this._promise.catch(reject);
    }

    getStatus() {
        return this._status;
    }
}
export default Process;
import { ProcessStatus } from './ProcessStatus';
import { Process } from './Process';

class ProcessQueue {
    constructor() {

        this._status = ProcessStatus.INPROGRESS;
        this._promise = new Promise(function (resolve, reject) {
            this._resolve = resolve;
            this._reject = reject;
        }.bind(this));

        this._processList = [];
        this._resolvedCount = 0;
        this._successCount = 0;
        this._failureCount = 0;

        this._successCallback = function() {
            this._resolvedCount++;
            this._successCount++;
            if (this._resolvedCount == this._processList.length) {
                if (this._failureCount == 0) {
                    this._status = ProcessStatus.SUCCESS;
                    this._resolve();
                } else {
                    this._status = ProcessStatus.FAILED;
                    this._reject();
                }
            }
        }

        this._failureCallback = function() {
            this._resolvedCount++;
            this._failureCount++;
            if (this._resolvedCount == this._processList.length) {
                this._status = ProcessStatus.FAILED;
                this._reject();
            }
        }
    }

    push(process) {
        if (process instanceof Process || process instanceof ProcessQueue) {
            process.then(this._successCallback, this._failureCallback);
        } else {
            throw new TypeError('Invalid process type pushed to the queue');
        }
        this._processList.push(process);
    }

    then(resolve, reject) {
        return this._promise.then(resolve, reject);
    }

    catch(reject) {
        return this._promise.catch(reject);
    }

    getStatus() {
        return this._status;
    }
}
export default ProcessQueue;
ProcessStatus = Object.freeze({
    'INPROGRESS': 'INPROGRESS',
    'SUCCESS': 'SUCCESS',
    'FAILED': 'FAILED'
});
export default ProcessStatus;
ProcessQueue.js

import { ProcessStatus } from './ProcessStatus';

class Process {
    constructor(invoker) {
        if (invoker instanceof Function) {
            this._promise = new Promise(invoker);
        } else if (invoker instanceof Promise) {
            this._promise = invoker;
        } else {
            throw new TypeError('Invalid invoker passed for Process constructor');
        }

        this._status = ProcessStatus.INPROGRESS;
        this._promise.then(function () {
            this._status = ProcessStatus.SUCCESS;
        }.bind(this), function () {
            this._status = ProcessStatus.FAILED;
        }.bind(this));

    }

    then(resolve, reject) {
        return this._promise.then(resolve, reject);
    }

    catch(reject) {
        return this._promise.catch(reject);
    }

    getStatus() {
        return this._status;
    }
}
export default Process;
import { ProcessStatus } from './ProcessStatus';
import { Process } from './Process';

class ProcessQueue {
    constructor() {

        this._status = ProcessStatus.INPROGRESS;
        this._promise = new Promise(function (resolve, reject) {
            this._resolve = resolve;
            this._reject = reject;
        }.bind(this));

        this._processList = [];
        this._resolvedCount = 0;
        this._successCount = 0;
        this._failureCount = 0;

        this._successCallback = function() {
            this._resolvedCount++;
            this._successCount++;
            if (this._resolvedCount == this._processList.length) {
                if (this._failureCount == 0) {
                    this._status = ProcessStatus.SUCCESS;
                    this._resolve();
                } else {
                    this._status = ProcessStatus.FAILED;
                    this._reject();
                }
            }
        }

        this._failureCallback = function() {
            this._resolvedCount++;
            this._failureCount++;
            if (this._resolvedCount == this._processList.length) {
                this._status = ProcessStatus.FAILED;
                this._reject();
            }
        }
    }

    push(process) {
        if (process instanceof Process || process instanceof ProcessQueue) {
            process.then(this._successCallback, this._failureCallback);
        } else {
            throw new TypeError('Invalid process type pushed to the queue');
        }
        this._processList.push(process);
    }

    then(resolve, reject) {
        return this._promise.then(resolve, reject);
    }

    catch(reject) {
        return this._promise.catch(reject);
    }

    getStatus() {
        return this._status;
    }
}
export default ProcessQueue;
ProcessStatus = Object.freeze({
    'INPROGRESS': 'INPROGRESS',
    'SUCCESS': 'SUCCESS',
    'FAILED': 'FAILED'
});
export default ProcessStatus;
ProcessStatus

import { ProcessStatus } from './ProcessStatus';

class Process {
    constructor(invoker) {
        if (invoker instanceof Function) {
            this._promise = new Promise(invoker);
        } else if (invoker instanceof Promise) {
            this._promise = invoker;
        } else {
            throw new TypeError('Invalid invoker passed for Process constructor');
        }

        this._status = ProcessStatus.INPROGRESS;
        this._promise.then(function () {
            this._status = ProcessStatus.SUCCESS;
        }.bind(this), function () {
            this._status = ProcessStatus.FAILED;
        }.bind(this));

    }

    then(resolve, reject) {
        return this._promise.then(resolve, reject);
    }

    catch(reject) {
        return this._promise.catch(reject);
    }

    getStatus() {
        return this._status;
    }
}
export default Process;
import { ProcessStatus } from './ProcessStatus';
import { Process } from './Process';

class ProcessQueue {
    constructor() {

        this._status = ProcessStatus.INPROGRESS;
        this._promise = new Promise(function (resolve, reject) {
            this._resolve = resolve;
            this._reject = reject;
        }.bind(this));

        this._processList = [];
        this._resolvedCount = 0;
        this._successCount = 0;
        this._failureCount = 0;

        this._successCallback = function() {
            this._resolvedCount++;
            this._successCount++;
            if (this._resolvedCount == this._processList.length) {
                if (this._failureCount == 0) {
                    this._status = ProcessStatus.SUCCESS;
                    this._resolve();
                } else {
                    this._status = ProcessStatus.FAILED;
                    this._reject();
                }
            }
        }

        this._failureCallback = function() {
            this._resolvedCount++;
            this._failureCount++;
            if (this._resolvedCount == this._processList.length) {
                this._status = ProcessStatus.FAILED;
                this._reject();
            }
        }
    }

    push(process) {
        if (process instanceof Process || process instanceof ProcessQueue) {
            process.then(this._successCallback, this._failureCallback);
        } else {
            throw new TypeError('Invalid process type pushed to the queue');
        }
        this._processList.push(process);
    }

    then(resolve, reject) {
        return this._promise.then(resolve, reject);
    }

    catch(reject) {
        return this._promise.catch(reject);
    }

    getStatus() {
        return this._status;
    }
}
export default ProcessQueue;
ProcessStatus = Object.freeze({
    'INPROGRESS': 'INPROGRESS',
    'SUCCESS': 'SUCCESS',
    'FAILED': 'FAILED'
});
export default ProcessStatus;

代码参考:

好的,那么您要做的是将DOM操作与代码解耦,然后将Promise处理模块移植到node.jsYes,@JaromandaX,我说对了。