var myfunc = function() {
setTimeout(function() {
console.log("log1");
setTimeout(function() {
console.log("log2");
setTimeout(function() {
console.log("log3");
}, 4000);
}, 3000);
}, 2000);
}
var func1 = function() {
setTimeout(func2, 2000);
};
var func2 = function() {
console.log("log1");
setTimeout(func3, 3000);
};
var func3 = function() {
console.log("log2");
setTimeout(func4, 4000);
};
var func4 = function() {
console.log("log3");
};
new Promise(function(res, rej) {
console.log(Date.now() + " start setTimeout");
setTimeout(res, 2000);
}).then(function() {
console.log(Date.now() + " timeout call back");
});
$ node promisTest.js 1450194136374 start setTimeout 1450194138391 timeout call back
new Promise(function(res, rej) {
console.log(Date.now() + " start setTimeout 1");
setTimeout(res, 2000);
}).then(function() {
console.log(Date.now() + " timeout 1 call back");
new Promise(function(res, rej) {
console.log(Date.now() + " start setTimeout 2");
setTimeout(res, 3000);
}).then(function() {
console.log(Date.now() + " timeout 2 call back");
})
});
$ node promisTest.js 1450194338710 start setTimeout 1 1450194340720 timeout 1 call back 1450194340720 start setTimeout 2 1450194343722 timeout 2 call back
new Promise(...).then(...) .then(...).then(...).then(...)...
new Promise(function(res, rej) {
console.log(Date.now() + " start setTimeout 1");
setTimeout(res, 2000);
}).then(function() {
console.log(Date.now() + " timeout 1 call back");
return 1024;
}).then(function(arg) {
console.log(Date.now() + " last onFulfilled return " + arg);
});
$ node promisTest.js 1450277122125 start setTimeout 1 1450277124129 timeout 1 call back 1450277124129 last onFulfilled return 1024
new Promise(function(res, rej) {
console.log(Date.now() + " start setTimeout 1");
setTimeout(res, 2000);
}).then(function() {
console.log(Date.now() + " timeout 1 call back");
return new Promise(function(res, rej) {
console.log(Date.now() + " start setTimeout 2");
setTimeout(res, 3000);
});
}).then(function() {
console.log(Date.now() + " timeout 2 call back");
});
$ node promisTest.js 1450277510275 start setTimeout 1 1450277512276 timeout 1 call back 1450277512276 start setTimeout 2 1450277515327 timeout 2 call back
new Promise(function(res, rej) {
console.log(Date.now() + " start setTimeout 1");
setTimeout(res, 2000);
}).then(function() {
console.log(Date.now() + " timeout 1 call back");
return new Promise(function(res, rej) {
console.log(Date.now() + " start setTimeout 2");
setTimeout(res, 3000);
});
}).then(function() {
console.log(Date.now() + " timeout 2 call back");
return new Promise(function(res, rej) {
console.log(Date.now() + " start setTimeout 3");
setTimeout(res, 4000);
});
}).then(function() {
console.log(Date.now() + " timeout 3 call back");
return new Promise(function(res, rej) {
console.log(Date.now() + " start setTimeout 4");
setTimeout(res, 5000);
});
}).then(function() {
console.log(Date.now() + " timeout 4 call back");
});
$ node promisTest.js 1450277902714 start setTimeout 1 1450277904722 timeout 1 call back 1450277904724 start setTimeout 2 1450277907725 timeout 2 call back 1450277907725 start setTimeout 3 1450277911730 timeout 3 call back 1450277911730 start setTimeout 4 1450277916744 timeout 4 call back
function timeout1() {
return new Promise(function(res, rej) {
console.log(Date.now() + " start timeout1");
setTimeout(res, 2000);
});
}
function timeout2() {
return new Promise(function(res, rej) {
console.log(Date.now() + " start timeout2");
setTimeout(res, 3000);
});
}
function timeout3() {
return new Promise(function(res, rej) {
console.log(Date.now() + " start timeout3");
setTimeout(res, 4000);
});
}
function timeout4() {
return new Promise(function(res, rej) {
console.log(Date.now() + " start timeout4");
setTimeout(res, 5000);
});
}
timeout1()
.then(timeout2)
.then(timeout3)
.then(timeout4)
.then(function() {
console.log(Date.now() + " timout4 callback");
});
$ node promisTest.js 1450278983342 start timeout1 1450278985343 start timeout2 1450278988351 start timeout3 1450278992356 start timeout4 1450278997370 timout4 callback
fs.readFile('/etc/passwd', function (err, data) {
if (err) throw err;
console.log(data);
});
function readFile(fileName) {
return new Promise(function(resolve, reject) {
fs.readFile(fileName, function (err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}
readFile('theFile.txt').then(
function(data) {
console.log(data);
},
function(err) {
throw err;
}
);
function callp1() {
console.log(Date.now() + " start callp1");
return new Promise(function(res, rej) {
setTimeout(res, 2000);
});
}
function callp2() {
console.log(Date.now() + " start callp2");
return new Promise(function(res, rej) {
setTimeout(function() {
res({arg1: 4, arg2: "arg2 value"});
}, 3000);
});
}
function callp3(arg) {
console.log(Date.now() + " start callp3 with arg = " + arg);
return new Promise(function(res, rej) {
setTimeout(function() {
res("callp3");
}, arg * 1000);
});
}
callp1().then(function() {
console.log(Date.now() + " callp1 return");
return callp2();
}).then(function(ret) {
console.log(Date.now() + " callp2 return with ret value = " + JSON.stringify(ret));
return callp3(ret.arg1);
}).then(function(ret) {
console.log(Date.now() + " callp3 return with ret value = " + ret);
})
$ node promisTest.js
1450191479575 start callp1
1450191481597 callp1 return
1450191481599 start callp2
1450191484605 callp2 return with ret value = {"arg1":4,"arg2":"arg2 value"}
1450191484605 start callp3 with arg = 4
1450191488610 callp3 return with ret value = callp3
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有