sle*_*ica 17 http node.js promise
节点框架通常通过(err, result)回调工作.
是否存在基于承诺的Node框架,具有健康的社区和积极的开发(如快递)?
wir*_*res 13
在HTTP客户端,有新的fetch API https://fetch.spec.whatwg.org/
fetch()允许你创建类似于XMLHttpRequest(XHR)的网络请求,主要的区别是[它]使用Promises,它可以实现更简单,更清晰的API,避免回调地狱,并且必须记住XMLHttpRequest的复杂API
(https://developers.google.com/web/updates/2015/03/introduction-to-fetch)
一些实现:
这里有一些示例代码:
fetch('/some/url', {method: 'get'})
.then(function(response) {
// rejoice \o/
})
.catch(function(err) {
// error :-(
});
Run Code Online (Sandbox Code Playgroud)
有https://github.com/mzabriskie/axios
基于Promise的HTTP客户端,用于浏览器和node.js
示例代码:
// Make a request for a user with a given ID
axios.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
.catch(function (response) {
console.log(response);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5907 次 |
| 最近记录: |