Promise<T>

Promise (JavaScript)

ヤクソクヨー

今よ!ファンタジスタドール

[66] Promise は、約束です。

仕様書

言語

[62] 次のような概念があります。

[63] 次のような API があります。

継承

[71] Promise継承が可能な設計にはなっています。しかし、 実際使ってみると難しく、避けるべきと考えられています。

継承したクラスを使うと、継承した約束と元の約束の2種類のオブジェクトが混在することになります。 開発者はどれが継承したものでどれがそうでないかを注意深く管理する必要がでてきます。 resolvethen などを活用すればするほど、 気づかないうちに開発者の想定と違った方のオブジェクトが作られているケースが生じたり、 種類は同じながら意図が違うオブジェクトになっていたりして、 理解が難しいコードになってしまいます。

[72] 例えば、 actionabort メソッドを持った約束子クラスオブジェクトを返すとします。

var promise = action (1);
// ...
... .then (function () {
  promise.abort ();
});

その後少し処理を追加する必要が生じたとします。

var promise = action (1).then (function () {
  return action (2);
});
// ...
... .then (function () {
  promise.abort ();
});

このとき abort は全く違うオブジェクトに対する操作になっています。 開発者にとってこのケースやその他の色々なケースですべて意図通りの動作をするように子クラスを設計するのは、なかなか大変そうです。

関連

[67] Promise.pmPerl 移植版です。

歴史

前史

[73] 色々な JavaScriptライブラリー約束やそれに類する機能が実装されるようになりました。

Promises/A+

[59] JavaScript ライブラリー約束の共通仕様が必要と考えられるようになり、 JavaScript を使うアプリケーション開発者らによって Promises/A+ として標準化が試みられました。

DOM Standard

[74] Promises/A+ に基づく形で DOM Standard約束の機能が追加されました。 当初は DOMFuture, Future と呼ばれましたが、後に Promise に改称されました。

[1] slightlyoff/DOMFuture · GitHub ( ( 版)) https://github.com/slightlyoff/DOMFuture

[2] IRC logs: freenode / #whatwg / 20130212 ( ( 版)) http://krijnhoetmer.nl/irc-logs/whatwg/20130212#l-184

[3] The Future is now. Thanks all! · 1268f9e · whatwg/dom ( ( 版)) https://github.com/whatwg/dom/commit/1268f9ead6413b21a03f266496c52295a70e38e7

[4] DOM Standard ( ( 版)) http://dom.spec.whatwg.org/#futures

[5] ISSUE-124: Futures / Order of parameters (was: Re: Request for JSON-LD API review) ( (Manu Sporny 著, 版)) http://lists.w3.org/Archives/Public/public-linked-json/2013Apr/0016.html

[6] Explaining Futures - Tab Completion ( ( 版)) http://www.xanthir.com/b4PY0

[7] 現在進行中の処理と未来の値を指し示すfuture/promise/deferred - DebugIto’s diary ( ( 版)) http://d.hatena.ne.jp/debug-ito/20130414/1365923218

[8] Futures and promises - Wikipedia, the free encyclopedia ( ( 版)) http://en.wikipedia.org/wiki/Futures_and_promises

[9] future.pl ( ( 版)) https://gist.github.com/wakaba/5494059

[10] DOM: Futures ( (Anne van Kesteren 著, 版)) http://lists.w3.org/Archives/Public/www-dom/2013JanMar/0217.html

[11] Rename Futures per TC39 discussion. Drop done() per same. · 7a741b9 · whatwg/dom ( 版) https://github.com/whatwg/dom/commit/7a741b953990a89c6d27532928fe7817c3b25528

[12] IRC logs: freenode / #whatwg / 20130606 ( ( 版)) http://krijnhoetmer.nl/irc-logs/whatwg/20130606#l-382

[13] IRC logs: freenode / #whatwg / 20130619 ( ( 版)) http://krijnhoetmer.nl/irc-logs/whatwg/20130619#l-400

[14] 3777b39d692fe553da1caeb017e1a40b90eef832 - chromium/blink - Git at Google ( ( 版)) https://chromium.googlesource.com/chromium/blink/+/3777b39d692fe553da1caeb017e1a40b90eef832

[15] IRC logs: freenode / #whatwg / 20130701 ( ( 版)) http://krijnhoetmer.nl/irc-logs/whatwg/20130701#l-216

[16] Bug 22522 – WebIDL, error handling, and promises ( ( 版)) https://www.w3.org/Bugs/Public/show_bug.cgi?id=22522

ECMAScript

[75] DOM StandardPromise は一旦は完成レベルに達したとされていましたが、 TC39 の議論も踏まえて再設計されることになりました。 DOM Standard の仕様は一旦削除され、 Promise の仕様は domenicGitHub リポジトリーで検討が続けられました。

[76] domenic が完成させた新しい Promise 仕様は ECMAScript 本体仕様に取り込まれ、 DOM Standard の規定は完全に削除されました。 WebPromise の統合に関する事項は Web IDL で規定されるようになりました。

[17] State of promises — Anne’s Blog ( ( 版)) http://annevankesteren.nl/2013/08/promises

[18] IRC logs: freenode / #whatwg / 20130905 ( ( 版)) http://krijnhoetmer.nl/irc-logs/whatwg/20130905

[19] Move Promises out of the specification until drafting elsewhere is compl... · dd65132 · whatwg/dom ( ( 版)) https://github.com/whatwg/dom/commit/dd6513252268ab76ba62bf645aa4633ca8d0722c

[20] promises-unwrapping/README.md at master · domenic/promises-unwrapping ( ( 版)) https://github.com/domenic/promises-unwrapping/blob/master/README.md

[21] Bug 22296 – Microtask and promises ( ( 版)) https://www.w3.org/Bugs/Public/show_bug.cgi?id=22296

[22] Promises: final steps ( ( 版)) https://mail.mozilla.org/pipermail/es-discuss/2013-September/033118.html

[23] Promises - CommonJS Spec Wiki ( ( 版)) http://wiki.commonjs.org/wiki/Promises

[24] IRC logs: freenode / #whatwg / 20130912 ( ( 版)) http://krijnhoetmer.nl/irc-logs/whatwg/20130912

[25] IRC logs: freenode / #whatwg / 20131015 ( ( 版)) http://krijnhoetmer.nl/irc-logs/whatwg/20131015

[26] IRC logs: freenode / #whatwg / 20131017 ( ( 版)) http://krijnhoetmer.nl/irc-logs/whatwg/20131017#l-366

[27] domenic/promises-unwrapping ( ( 版)) https://github.com/domenic/promises-unwrapping/

[28] IRC logs: freenode / #whatwg / 20131122 ( ( 版)) http://krijnhoetmer.nl/irc-logs/whatwg/20131122

[29] JSON-LD 1.0 Processing Algorithms and API ( ( 版)) http://www.w3.org/TR/json-ld-api/#the-application-programming-interface

[30] ECMAScript Language Specification ECMA-262 6th Edition – DRAFT ( ( 版)) https://people.mozilla.org/~jorendorff/es6-draft.html#sec-promise-objects

[31] Promises are now ES6/IDL territory · 3f656c5 · whatwg/dom ( ( 版)) https://github.com/whatwg/dom/commit/3f656c52696dd2d90d3796569cc6dc18e73a487b

[32] Promises are now ES6/IDL territory · 3f656c5 · whatwg/dom ( ( 版)) https://github.com/whatwg/dom/commit/3f656c52696dd2d90d3796569cc6dc18e73a487b

[33] IRC logs: freenode / #whatwg / 20140513 ( ( 版)) http://krijnhoetmer.nl/irc-logs/whatwg/20140513

[34] Copy of Promises Spec Draft as of October 2013 ( ( 版)) http://www.w3.org/2013/10/json-ld-api/snapshot-promises-draft/

[35] IRC logs: freenode / #whatwg / 20140613 ( ( 版)) http://krijnhoetmer.nl/irc-logs/whatwg/20140613#l-594

[36] The "initialization" steps for Web browsers ( ( 版)) https://mail.mozilla.org/pipermail/es-discuss/2014-June/037559.html

[39] Web IDL にも Promise<T> が追加されました。

[40] なお Web IDLPromise<T> が追加される前に一部の仕様書では Promise という内側型なしの構文を使っていました。

現行 Web IDL 仕様ではこれは認められていません。

[41] Bug 26517 – Methods that return promises are unable to throw exceptions ( ( 版)) https://www.w3.org/Bugs/Public/show_bug.cgi?id=26517

[42] IRC logs: freenode / #whatwg / 20140809 ( ( 版)) http://krijnhoetmer.nl/irc-logs/whatwg/20140809

[45] Web Applications 1.0 r8804 Integrate with new promise stuff better ( ( 版)) https://html5.org/r/8804

[46] Quota Management API ( ( 版)) https://dvcs.w3.org/hg/quota/raw-file/tip/Overview.html

[48] Re: Strawman Promises consensus position, based on Thursday's telechat ( (Eric Rescorla 著, 版)) http://lists.w3.org/Archives/Public/public-media-capture/2014Oct/0047.html

[49] Intent to implement: Promise.when & Promise.isPromise - Google グループ ( ( 版)) https://groups.google.com/forum/#!topic/mozilla.dev.webapi/nW4KoL-6x7A

[50] IRC logs: freenode / #whatwg / 20141030 ( ( 版)) http://krijnhoetmer.nl/irc-logs/whatwg/20141030

[51] Bug 26939 – Information returned on MediaStreamTrack ended event ( ( 版)) https://www.w3.org/Bugs/Public/show_bug.cgi?id=26939

取消可能約束

[52] 取消可能約束参照。

[53] wakaba/perl-promise ( 版) https://github.com/wakaba/perl-promise

[54] 1152902 – Add a fast path to promises for being resolved with a promise ( 版) https://bugzilla.mozilla.org/show_bug.cgi?id=1152902

[55] 1156797 – consider exposing a "return value ignored" signal to c++ bindings ( 版) https://bugzilla.mozilla.org/show_bug.cgi?id=1156797

[56] 1162013 – Microtask scheduled via Promise should run right at the end of the current setTimeout handler even if other setTimeout timers have also expired ( 版) https://bugzilla.mozilla.org/show_bug.cgi?id=1162013

[60] Promises are now defined by IDL · whatwg/html@2530b44 ( 版) https://github.com/whatwg/html/commit/2530b4400b81d7b7178d22a8202de7ed17a3591a

[44] Make HTMLMediaElement.prototype.play() return a promise · whatwg/html@be8edde ( 版) https://github.com/whatwg/html/commit/be8edde20ac46786af968d4de23e90801d4c7213

[57] Removed promise antipattern from code example by bergus · Pull Request #227 · whatwg/fetch ( 版) https://github.com/whatwg/fetch/pull/227

[58] Merge pull request #113 from domenic/modernize-invoke ( (bzbarsky著, )) https://github.com/heycam/webidl/commit/09c011d8de1077fe5991ceded97838650e376f6c

[61] Issue 627309 - chromium - IDL bindings: Calling a promise-returning method with this=null should reject, not throw - Monorail () https://bugs.chromium.org/p/chromium/issues/detail?id=627309&desc=2

[64] 28057 – Support Promise subclass () https://www.w3.org/Bugs/Public/show_bug.cgi?id=28057

[65] Disallow nullable Promise types. (#248) (tobie著, ) https://github.com/heycam/webidl/commit/7366419820e871db6792fd4e7407d490cdaa8020

[68] Update attribute setters and getters (#217) (domenic著, ) https://github.com/heycam/webidl/commit/4c9c298c9c37ce5e8da59c2d9becd943703fa06b

[69] Fix incorrect [LenientThis] that should be [LenientSetter] (#272) (domenic著, ) https://github.com/heycam/webidl/commit/fef2cdaeb259e79e01b4c2706af1f541e56d4ffe

[70] Don't <dfn> Promise. WebIDL already does that. (#1023) (jyasskin著, ) https://github.com/w3c/csswg-drafts/commit/7ff67b227854ab4f37fe0214de41510198f26685

[77] Deprecations and removals in Chrome 63  |  Web  |  Google Developers () https://developers.google.com/web/updates/2017/10/chrome-63-deprecations

[78] Promise are part of WebIDL. (mounirlamouri著, ) https://github.com/w3c/screen-orientation/commit/fe3aed325d596c6bd90e27d67d99895013ec8a84

[79] 27995 – Consider adding some sort of shorthand for resolving a promise with an IDL value () https://www.w3.org/Bugs/Public/show_bug.cgi?id=27995

[80] [web-animations-1] Should async playback methods return the `ready` Promise? · Issue #2206 · w3c/csswg-drafts () https://github.com/w3c/csswg-drafts/issues/2206

[81] Don't create { value, done } objects with Object.prototype for intern… (ricea著, ) https://github.com/whatwg/streams/commit/a27a1fd0f8640d6054e0cbc8b6d7f5464dd096ab

[82] Setting Object.prototype.then permits interfering with pipeTo() internals · Issue #933 · whatwg/streams () https://github.com/whatwg/streams/issues/933

[83] Don't create iterators with Object.prototype for internal use by ricea · Pull Request #936 · whatwg/streams () https://github.com/whatwg/streams/pull/936

[84] chain of never resolved promises create memory leaks · Issue #179 · promises-aplus/promises-spec () https://github.com/promises-aplus/promises-spec/issues/179

[85] recursion - Building a promise chain recursively in javascript - memory considerations - Stack Overflow () https://stackoverflow.com/questions/29925948/building-a-promise-chain-recursively-in-javascript-memory-considerations

[86] 5002 - promises leak memory - v8 - Monorail () https://bugs.chromium.org/p/v8/issues/detail?id=5002

[87] Use %PromiseProto_then% to 'perform some steps once a promise is sett… (Ms2ger著, ) https://github.com/heycam/webidl/commit/935601a15d8775afef828935b578ea17c29f36bf

[88] Remove unnecessary epsilon from PromiseType case. (Ms2ger著, ) https://github.com/heycam/webidl/commit/115f37ba6cd84a0ef88cb8ad76bd7a2c1f2dec8a

[89] Disallow Promise types in unions at the syntax level. by Ms2ger · Pull Request #746 · heycam/webidl () https://github.com/heycam/webidl/pull/746

[90] Disallow Promise types in unions at the syntax level. (Ms2ger著, ) https://github.com/heycam/webidl/commit/cc5b4aa81b7d0e5582d81860b83d6f286f7749df

[91] Disallow Promise types in unions at the syntax level. by Ms2ger · Pull Request #746 · heycam/webidl () https://github.com/heycam/webidl/pull/746

[92] NonAnyType vs SingleType as home of PromiseType ε alternative · Issue #740 · heycam/webidl () https://github.com/heycam/webidl/issues/740

[93] Improve the definition of the void type. by Ms2ger · Pull Request #757 · heycam/webidl () https://github.com/heycam/webidl/pull/757

[94] Looking for some clarity on Promise<void> · Issue #737 · heycam/webidl () https://github.com/heycam/webidl/issues/737

[95] Import the Promises Guide. (Ms2ger著, ) https://github.com/heycam/webidl/commit/68dbd237975f745dfaa3070ebc018ad46d13b0cf

[96] Import the Promises Guide. by Ms2ger · Pull Request #772 · heycam/webidl () https://github.com/heycam/webidl/pull/772

[97] Import (and fix) the promises guide · Issue #490 · heycam/webidl () https://github.com/heycam/webidl/issues/490

[98] Editorial: Update 'wait for all' usage. (#1463) (Ms2ger著, ) https://github.com/w3c/ServiceWorker/commit/278c48b14ca0c5c89a5953237fb3c921b256303e

[99] Editorial: Update 'wait for all' usage. by Ms2ger · Pull Request #1463 · w3c/ServiceWorker () https://github.com/w3c/ServiceWorker/pull/1463

[100] "waiting for all" definition changed · Issue #1363 · w3c/ServiceWorker () https://github.com/w3c/ServiceWorker/issues/1363

[101] Clarify "waiting for all" in Install · Issue #1461 · w3c/ServiceWorker () https://github.com/w3c/ServiceWorker/issues/1461

[102] Fix example for 'upon settling'. (Ms2ger著, ) https://github.com/heycam/webidl/commit/a22602267449c3c825814316c918c9a6aea48991

[103] Fix example for 'upon settling'. by Ms2ger · Pull Request #790 · heycam/webidl () https://github.com/heycam/webidl/pull/790

[104] "Upon settling" example incorrect · Issue #788 · heycam/webidl () https://github.com/heycam/webidl/issues/788

[105] Use "react" for the spec promise.then() analog (domenic著, ) https://github.com/heycam/webidl/commit/db74ef4ac87cd095803ca220954ecb09ae15a87c

[106] Use "react" for the spec promise.then() analog by domenic · Pull Request #792 · heycam/webidl () https://github.com/heycam/webidl/pull/792

[107] "Perform some steps once a promise is settled" awkward to use in return value form · Issue #783 · heycam/webidl () https://github.com/heycam/webidl/issues/783

[108] Updating use of promises (#1467) (jakearchibald著, ) https://github.com/w3c/ServiceWorker/commit/667ba40de290b5401baac38e5f769ccbf3812f76

[109] Updating use of promises by jakearchibald · Pull Request #1467 · w3c/ServiceWorker () https://github.com/w3c/ServiceWorker/pull/1467

[110] Editorial: stop using "promise-calling" (annevk著, ) https://github.com/whatwg/fetch/commit/ce287b002e29f231d3bfc6bdc92dbee74377a64d

[111] Editorial: stop using "promise-calling" by annevk · Pull Request #937 · whatwg/fetch () https://github.com/whatwg/fetch/pull/937

[112] Meta: change promises-guide references to Web IDL (MattiasBuelens, , ) https://github.com/whatwg/streams/commit/d26db0ea6ab72bee56b3d5c4512349fb028fa04d

[113] Links to "a new promise", "a promise resolved with", etc. are broken · Issue #1020 · whatwg/streams () https://github.com/whatwg/streams/issues/1020

[114] Change promises-guide references to Web IDL by MattiasBuelens · Pull Request #1021 · whatwg/streams () https://github.com/whatwg/streams/pull/1021