javascript Reactjs 承诺,我们应该如何使用它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31840412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reactjs promises, how should we use it?
提问by Non
I've been reading a lot about React for the last 3 days, but I don't see much information about the use of promises, so I have that concern.
在过去的 3 天里,我一直在阅读大量关于 React 的文章,但是我没有看到太多关于使用 Promise 的信息,所以我有这个担忧。
Is there any library for this?
有没有这方面的图书馆?
How should I use promises in React?
我应该如何在 React 中使用 Promise?
采纳答案by sma
React doesn't come with a promise library baked in like Angular with $http
. You will have to find your own.
React 没有像 Angular 那样带有$http
. 你将不得不找到你自己的。
A few you can try:
你可以尝试一些:
- Bluebird(personal recommendation)
- jQuery's
$ajax
- Native promises (unless you actually have to support IE): http://caniuse.com/#feat=promises
- 蓝鸟(个人推荐)
- jQuery的
$ajax
- 本机承诺(除非您确实必须支持 IE):http: //caniuse.com/#feat=promises
回答by Neel Patel
Promise object is used for handling asynchronous computations which has some important guarantees that are difficult to handle with the callback method (the more old-school method of handling asynchronous code).
Promise 对象用于处理异步计算,它具有一些难以用回调方法处理的重要保证(处理异步代码的更老派的方法)。
A Promise object is simply a wrapper around a value that may or may not be known when the object is instantiated and provides a method for handling the value after it is known (also known as resolved) or is unavailable for a failure reason (we'll refer to this as rejected).
Promise 对象只是一个值的包装器,该值在对象被实例化时可能已知也可能未知,并提供了一种方法来处理已知(也称为已解决)或由于失败原因不可用(我们我将此称为被拒绝)。
Using a Promise object gives us the opportunity to associate functionality for an asynchronous operation's eventual success or failure (for whatever reason). It also allows us to treat these complex scenarios by using synchronous.
使用 Promise 对象让我们有机会为异步操作的最终成功或失败(无论出于何种原因)关联功能。它还允许我们使用同步来处理这些复杂的场景。
To see more at : https://www.npmjs.com/package/react-promise