ajs*_*sie 146 javascript node.js promise
根据我的理解,有三种方法可以调用异步代码:
request.on("event", callback);fs.open(path, flags, mode, callback);我找到了一个承诺库https://github.com/kriszyp/node-promise,但我没有得到它.
有人可以解释一下什么是承诺,为什么我应该使用它?
另外,为什么它从Node.js中删除了?
eny*_*nyo 98
由于这个问题仍然有很多观点(像我的一样),我想指出:
Hri*_*shi 19
承诺是一个"事物",它代表了操作的"最终"结果.这里需要注意的是,它抽象出事情发生时的细节,并让你专注于事情发生后应该发生的事情.这将产生干净,可维护的代码,而不是在回调内部的回调内部进行回调,您的代码看起来有点像:
var request = new Promise(function(resolve, reject) {
//do an ajax call here. or a database request or whatever.
//depending on its results, either call resolve(value) or reject(error)
//where value is the thing which the operation's successful execution returns and
//error is the thing which the operation's failure returns.
});
request.then(function successHandler(result) {
//do something with the result
}, function failureHandler(error) {
//handle
});
Run Code Online (Sandbox Code Playgroud)
承诺的规范说明了承诺
then
Run Code Online (Sandbox Code Playgroud)
方法应返回在给定的successHandler或failureHandler回调完成时满足的新promise.这意味着当您有一组需要执行的异步任务时,您可以将promise链接在一起,并确保操作顺序得到保证,就像您使用了回调一样.因此,不是在回调内部的回调内传递回调,而是具有链式承诺的代码如下所示:
var doStuff = firstAsyncFunction(url) {
return new Promise(function(resolve, reject) {
$.ajax({
url: url,
success: function(data) {
resolve(data);
},
error: function(err) {
reject(err);
}
});
};
doStuff
.then(secondAsyncFunction) //returns a promise
.then(thirdAsyncFunction); //returns a promise
Run Code Online (Sandbox Code Playgroud)
要了解更多关于承诺及其超酷的原因,请查看Domenic的博客:http://domenic.me/2012/10/14/youre-missing-the-point-of-promises/
Mike Taulty 有一系列视频,每个视频不到十分钟,描述了WinJS Promise库的工作原理.
这些视频非常有用,Mike通过一些精心挑选的代码示例设法展示了Promise API的强大功能.
var twitterUrl = "http://search.twitter.com/search.json?q=windows";
var promise = WinJS.xhr({ url: twitterUrl });
promise = promise.then(
function (xhr) {
},
function (xhr) {
// handle error
});
Run Code Online (Sandbox Code Playgroud)
如何处理例外的处理特别好.
尽管有WinJs参考,但这是一个普遍感兴趣的视频系列,因为Promise API在其众多实现中大致相似.
RSVP是一个轻量级的Promise实现,它通过了Promise/A +测试套件.我非常喜欢API,因为它的风格类似于WinJS界面.
2014年4月更新
顺便说一句,WinJS库现在是开源的.
Promise的另一个优点是,错误处理以及引发和捕获异常的方法比尝试使用回调处理要好得多。
在蓝鸟库实现承诺,并为您提供一流长的堆栈跟踪,速度非常快,并警告有关未被捕获的错误。根据http://bluebirdjs.com/docs/benchmarks.html的介绍,与其他promise库相比,它还更快,使用的内存更少。
| 归档时间: |
|
| 查看次数: |
95776 次 |
| 最近记录: |