Is the deferred/promise concept in JavaScript a ne

2020-01-31 01:57发布

问题:

I really like the idea of jQuery's deferred/promise pattern or paradigm but sometimes I have trouble wrapping my aging brain around the finer points or specific implementation details.

In fact recently I've found that the deferred/promise pattern/paradigm seems to predate jQuery and is also in at least these other JavaScript libraries/frameworks:

  • Deferred github
  • Q homepage
  • task.js homepage
  • when.js github
  • wire.js github, presentation
  • YUI gallery-deferred module

I've probably missed some, included stuff that's really part of one of the others, and made other mistakes in that list... Please edit it to correct it or leave comments.

Was the deferred/promise concept invented by "Q"? Was it invented with some other JavaScript library or framework? Or does it predate JavaScript completely?

If the whole idea has traditionally been part of functional programming, where was it first done and where can I read about the concept generally rather than these various different JavaScript implementations?

Or is functional programming entirely beside the point? Is the concept more related to concurrent programming than to functional programming?

回答1:

According to Wikipedia: The term promise was proposed in 1976 by Daniel P. Friedman and David Wise, and Peter Hibbard called it eventual. A somewhat similar concept future was introduced in 1977 in a paper by Henry Baker and Carl Hewitt.

See: Futures and promises history on Wikipedia:

The future and/or promise constructs were first implemented in programming languages such as MultiLisp and Act 1. The use of logic variables for communication in concurrent logic programming languages was quite similar to futures. These started with Prolog with Freeze and IC Prolog, and became a true concurrency primitive with Relational Language, Concurrent Prolog, Guarded Horn Clauses (GHC), Parlog, Vulcan, Janus, Mozart/Oz, Flow Java, and Alice ML. The single-assignment I-var from dataflow programming languages, originating in Id and included in Reppy's Concurrent ML, is much like the concurrent logic variable.

The promise pipelining technique (using futures to overcome latency) was invented by Barbara Liskov and Liuba Shrira in 1988,[12] and independently by Mark S. Miller, Dean Tribble and Rob Jellinghaus in the context of Project Xanadu circa 1989.[13]

The term promise was coined by Liskov and Shrira, although they referred to the pipelining mechanism by the name call-stream, which is now rarely used. [...]



回答2:

I think in JavaScript deferred/promise concept first appeared in Dojo (inspired by Python's Twisted) then in jQuery.

The notable moment is work of CommonJS group which put a lot effort to "standarize" promises for JavaScript, see A, B and D proposals, on that basis Kris Kowal's Q (I guess currently most popular implementation) evolved.

Deferred (I'm the author) is later implementation, it was initially inspired by Q, its goal is to make promises feel natural in JavaScript, and also to make transition from Node.js callback style easy.

But yes as @rsp wrote, promises are much older concept than JavaScript itself :)