代碼:https://github.com/fengchunjian/nodejs_examples/tree/master/promise
Promise01
npm install node-fetch -g
//promise01.js
var fetch = require("node-fetch");
fetch("https://api.github.com")
.then(function(res) {
return res.json();
}).then(function(json) {
console.log(json);
});
Promise02
//promise02.js
unction fetchPage() {
console.log("fetchPage");
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve("Page data");
}, 1000);
});
}
fetchPage().then(function(data) {
console.log(data);
});
參考文檔
Pormise
http://edu.51cto.com/center/course/lesson/index?id=169729
初探Promise
https://segmentfault.com/a/1190000007032448