Promise

代码: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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容