zhufengnodejs / 201601node Goto Github PK
View Code? Open in Web Editor NEW这是2016年第一期正式课项目
License: MIT License
这是2016年第一期正式课项目
License: MIT License
63342里webstorm预览html文件时的端口。
如果你新建一个node服务器,需要访问此服务器的时候,需要通过访问这个NODE服务器的监听端口来进行访问,而不能访问63342端口。
var fs = require('fs');
function copy(src,target){
fs.readFile(src,function(err,data){
if(!err){
var dataToCopy = data;
fs.writeFile(target,dataToCopy,function(err,data){
if(err){
console.log(err);
}
})
}
});
}
copy("index.txt","temp.txt");
请参考 api文档或阅读源码
http://nodeapi.ucdok.com/#/api/querystring.html
一种实现代码
function Promise(executor) {
let self = this;
self.status = 'pending';
self.value = undefined;
self.onResolvedCallbacks = [];
self.onRejectedCallbacks = [];
function resolve(value) {
if (value instanceof Promise)
return value.then(resolve, reject);
setTimeout(function () {
if (self.status == 'pending') {
self.status = 'fulfilled';
self.value = value;
self.onResolvedCallbacks.forEach(item => item(value));
}
});
}
function reject(reason) {
setTimeout(function () {
if (self.status == 'pending') {
self.status = 'rejected';
self.value = reason;
self.onRejectedCallbacks.forEach(item => item(reason));
}
});
}
try {
executor(resolve, reject);
} catch (e) {
reject(e);
}
}
function resolvePromise(promise2, x, resolve, reject) {
if (x === promise2)
return reject(new TypeError('死循环'));
let then, called = false;
if (x instanceof Promise) {
if (x.status == "pending") {
x.then(function (y) {
resolvePromise(promise2, y, resolve, reject);
}, reject);
} else {
x.then(resolve, reject);
}
} else if (x != null && ((typeof x == 'object' || typeof x == 'function'))) {
try {
then = x.then;
if(typeof then == 'function'){
then.call(x, function (y) {
if (called) return;
called = true;
resolvePromise(promise2, y, resolve, reject);
}, function (y) {
if (called) return;
called = true;
reject(y);
});
}else{
resolve(x);
}
} catch (e) {
if (called) return;
called = true;
reject(e);
}
} else {
resolve(x);
}
}
Promise.prototype.then = function (onFulfilled, onRejected) {
let self = this;
var promise2;
onFulfilled = typeof onFulfilled == 'function' ? onFulfilled : value => value;
onRejected = typeof onRejected == 'function' ? onRejected : reason => {
throw reason
};
if (self.status == 'fulfilled') {
return promise2 = new Promise(function (resolve, reject) {
setTimeout(function() {
try {
let x = onFulfilled(self.value);
resolvePromise(promise2, x, resolve, reject)
} catch (e) {
reject(e);
}
});
});
}
if (self.status == 'rejected') {
return promise2 = new Promise(function (resolve, reject) {
setTimeout(function() {
try {
let x = onRejected(self.value);
resolvePromise(promise2, x, resolve, reject)
} catch (e) {
reject(e);
}
});
});
}
if (self.status == 'pending') {
return promise2 = new Promise(function (resolve, reject) {
self.onResolvedCallbacks.push(function () {
try {
let x = onFulfilled(self.value);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
});
self.onRejectedCallbacks.push(function () {
try {
let x = onRejected(self.value);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
});
});
}
}
Promise.prototype.catch = function (onRejected) {
return this.then(null, onRejected)
}
Promise.deferred = Promise.defer = function () {
var dfd = {}
dfd.promise = new Promise(function (resolve, reject) {
dfd.resolve = resolve
dfd.reject = reject
})
return dfd
}
module.exports = Promise;
我有以下问题
x === promise2
什么时候会出现?为什么会循环依赖?typeof x == 'function'
什么时候x会是一个对象A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.