Future and promise
A future is a result of a computation that may not be available yet. Examples include:
未来是计算的结果,可能尚未可用,例如:
a data buffer that we are reading from the network 我们从网络中读取的数据缓冲区
the expiration of a timer 计时器的期限
the completion of a disk write 完成磁盘写入
the result computation that requires the values from one or more other futures. 需要一个或多个其他期货价值的结果计算
a promise is an object or function that provides you with a future, with the expectation that it will fulfill the future.
一个承诺是一个目标或者功能,它为你提供了一个未来,期望它能够实现未来。
Promises and futures simplify asynchronous programming since they decouple the event producer (the promise) and the event consumer (whoever uses the future). Whether the promise is fulfilled before the future is consumed, or vice versa, does not change the outcome of the code.
允诺和未来简化了异步编程,因为它们将事件生产者(允诺)和事件消费者(使用未来的人)分离开来。 无论承诺是否在未来消耗之前实现,或者反之亦然,都不会改变代码的结果。
Consuming a future