-
Notifications
You must be signed in to change notification settings - Fork 4
/
303.txt
381 lines (279 loc) · 15.9 KB
/
303.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
[FIG(pull-quote)[
> [[ヤクソクヨー]]
[FIGCAPTION[
[CITE[今よ!ファンタジスタドール]]
]FIGCAPTION]
]FIG]
[66] [CODE(JS)@en[Promise]] は、[[約束]]です。
* 仕様書
[REFS[
- [43] [CITE[ECMAScript® 2016 Language Specification]] ([TIME[2015-12-21 08:35:42 +09:00]] 版) <https://tc39.github.io/ecma262/#sec-promise-objects>
]REFS]
* 言語
[62] 次のような概念があります。
[FIG(short list)[
- [[resolve]]
- [[reject]]
- [CODE[PromiseJobs]]
- [[約束コールバックの実行]]
]FIG]
[63] 次のような [[API]] があります。
[FIG(short list)[
- [CODE(JS)@en[[[Promise]]]]
- [CODE(JS)@en[[[resolve]]]]
- [CODE(JS)@en[[[reject]]]]
- [CODE(JS)@en[[[then]]]]
- [CODE(JS)@en[[[catch]]]]
]FIG]
* 継承
[71] [CODE[Promise]] は[[継承]]が可能な設計にはなっています。しかし、
実際使ってみると難しく、避けるべきと考えられています。
[[継承]]した[[クラス]]を使うと、[[継承]]した[[約束]]と元の[[約束]]の2種類の[[オブジェクト]]が混在することになります。
開発者はどれが[[継承]]したものでどれがそうでないかを注意深く管理する必要がでてきます。
[CODE[resolve]] や [CODE[then]] などを活用すればするほど、
気づかないうちに開発者の想定と違った方の[[オブジェクト]]が作られているケースが生じたり、
種類は同じながら意図が違う[[オブジェクト]]になっていたりして、
理解が難しいコードになってしまいます。
[EG[
[72] 例えば、 [CODE[action]] が [CODE[abort]] メソッドを持った[[約束]]の[[子クラス]]の[[オブジェクト]]を返すとします。
[PRE(code)[
var promise = action (1);
// ...
... .then (function () {
promise.abort ();
});
]PRE]
その後少し処理を追加する必要が生じたとします。
[PRE(code)[
var promise = action (1).then (function () {
return action (2);
});
// ...
... .then (function () {
promise.abort ();
});
]PRE]
このとき [CODE[abort]] は全く違うオブジェクトに対する操作になっています。
開発者にとってこのケースやその他の色々なケースですべて意図通りの動作をするように子クラスを設計するのは、なかなか大変そうです。
]EG]
* 関連
[67] [CODE[Promise.pm]] は [[Perl]] 移植版です。
* 歴史
** 前史
[73] 色々な [[JavaScript]] の[[ライブラリー]]で[[約束]]やそれに類する機能が実装されるようになりました。
** Promises/A+
[59] [[JavaScript]] [[ライブラリー]]の[[約束]]の共通仕様が必要と考えられるようになり、
[[JavaScript]] を使う[[アプリケーション]]開発者らによって [[Promises/A+]]
として[[標準化]]が試みられました。
** DOM Standard
[74] [[Promises/A+]] に基づく形で [CITE[DOM Standard]] に[[約束]]の機能が追加されました。
当初は [DFN[[CODE[DOMFutur]]e]], [DFN[[CODE[Future]]]] と呼ばれましたが、後に
[CODE[Promise]] に改称されました。
[1] [CITE[slightlyoff/DOMFuture · GitHub]]
( ([TIME[2013-02-16 19:15:25 +09:00]] 版))
<https://github.com/slightlyoff/DOMFuture>
[2] [CITE[IRC logs: freenode / #whatwg / 20130212]]
( ([TIME[2013-02-16 18:57:01 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20130212#l-184>
[3] [CITE[The Future is now. Thanks all! · 1268f9e · whatwg/dom]]
( ([TIME[2013-04-04 09:15:21 +09:00]] 版))
<https://github.com/whatwg/dom/commit/1268f9ead6413b21a03f266496c52295a70e38e7>
[4] [CITE@en-US[DOM Standard]]
( ([TIME[2013-04-03 18:58:33 +09:00]] 版))
<http://dom.spec.whatwg.org/#futures>
[5] [CITE@en[ISSUE-124: Futures / Order of parameters (was: Re: Request for JSON-LD API review)]]
( ([[Manu Sporny]] 著, [TIME[2013-04-17 23:50:35 +09:00]] 版))
<http://lists.w3.org/Archives/Public/public-linked-json/2013Apr/0016.html>
[6] [CITE[Explaining Futures - Tab Completion]]
( ([TIME[2013-04-24 00:08:18 +09:00]] 版))
<http://www.xanthir.com/b4PY0>
[7] [CITE[現在進行中の処理と未来の値を指し示すfuture/promise/deferred - DebugIto’s diary]]
( ([TIME[2013-04-14 07:06:58 +09:00]] 版))
<http://d.hatena.ne.jp/debug-ito/20130414/1365923218>
[8] [CITE@en[Futures and promises - Wikipedia, the free encyclopedia]]
( ([TIME[2013-04-19 23:14:57 +09:00]] 版))
<http://en.wikipedia.org/wiki/Futures_and_promises>
[9] [CITE[future.pl]]
( ([TIME[2013-05-01 07:00:00 +09:00]] 版))
<https://gist.github.com/wakaba/5494059>
[10] [CITE@en[DOM: Futures]]
( ([[Anne van Kesteren]] 著, [TIME[2013-03-29 01:04:09 +09:00]] 版))
<http://lists.w3.org/Archives/Public/www-dom/2013JanMar/0217.html>
[11] [CITE[Rename Futures per TC39 discussion. Drop done() per same. · 7a741b9 · whatwg/dom]] ([TIME[2013-06-06 13:27:31 +09:00]] 版) <https://github.com/whatwg/dom/commit/7a741b953990a89c6d27532928fe7817c3b25528>
[12] [CITE[IRC logs: freenode / #whatwg / 20130606]]
( ([TIME[2013-06-09 01:37:38 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20130606#l-382>
[13] [CITE[IRC logs: freenode / #whatwg / 20130619]]
( ([TIME[2013-06-21 08:59:12 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20130619#l-400>
[14] [CITE[3777b39d692fe553da1caeb017e1a40b90eef832 - chromium/blink - Git at Google]]
( ([TIME[2013-06-25 01:22:03 +09:00]] 版))
<https://chromium.googlesource.com/chromium/blink/+/3777b39d692fe553da1caeb017e1a40b90eef832>
[15] [CITE[IRC logs: freenode / #whatwg / 20130701]]
( ([TIME[2013-07-02 23:22:48 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20130701#l-216>
[16] [CITE@en[Bug 22522 – WebIDL, error handling, and promises]]
( ([TIME[2013-07-02 23:40:42 +09:00]] 版))
<https://www.w3.org/Bugs/Public/show_bug.cgi?id=22522>
** ECMAScript
[75] [CITE[DOM Standard]] の [CODE[Promise]] は一旦は完成レベルに達したとされていましたが、
[[TC39]] の議論も踏まえて再設計されることになりました。 [CITE[DOM Standard]]
の仕様は一旦削除され、 [CODE[Promise]] の仕様は [[domenic]] の
[[GitHub]] [[リポジトリー]]で検討が続けられました。
[76] [[domenic]] が完成させた新しい [CODE[Promise]] 仕様は [[ECMAScript]]
本体仕様に取り込まれ、 [CITE[DOM Standard]] の規定は完全に削除されました。
[[Web]] と [CODE[Promise]] の統合に関する事項は [CITE[Web IDL]]
で規定されるようになりました。
[17] [CITE[State of promises — Anne’s Blog]]
( ([TIME[2013-08-28 16:05:18 +09:00]] 版))
<http://annevankesteren.nl/2013/08/promises>
[18] [CITE[IRC logs: freenode / #whatwg / 20130905]]
( ([TIME[2013-09-09 00:06:40 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20130905>
[19] [CITE[Move Promises out of the specification until drafting elsewhere is compl... · dd65132 · whatwg/dom]]
( ([TIME[2013-09-13 07:28:09 +09:00]] 版))
<https://github.com/whatwg/dom/commit/dd6513252268ab76ba62bf645aa4633ca8d0722c>
[20] [CITE[promises-unwrapping/README.md at master · domenic/promises-unwrapping]]
( ([TIME[2013-09-13 07:29:20 +09:00]] 版))
<https://github.com/domenic/promises-unwrapping/blob/master/README.md>
[21] [CITE@en[Bug 22296 – Microtask and promises]]
( ([TIME[2013-09-13 07:29:44 +09:00]] 版))
<https://www.w3.org/Bugs/Public/show_bug.cgi?id=22296>
[22] [CITE[Promises: final steps]]
( ([TIME[2013-09-04 18:41:19 +09:00]] 版))
<https://mail.mozilla.org/pipermail/es-discuss/2013-September/033118.html>
[23] [CITE@en[Promises - CommonJS Spec Wiki]]
( ([TIME[2012-11-16 02:09:26 +09:00]] 版))
<http://wiki.commonjs.org/wiki/Promises>
[24] [CITE[IRC logs: freenode / #whatwg / 20130912]]
( ([TIME[2013-09-15 16:17:53 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20130912>
[25] [CITE[IRC logs: freenode / #whatwg / 20131015]]
( ([TIME[2013-10-17 22:04:42 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20131015>
[26] [CITE[IRC logs: freenode / #whatwg / 20131017]]
( ([TIME[2013-10-21 00:46:53 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20131017#l-366>
[27] [CITE[domenic/promises-unwrapping]]
( ([TIME[2013-11-17 02:24:45 +09:00]] 版))
<https://github.com/domenic/promises-unwrapping/>
[28] [CITE[IRC logs: freenode / #whatwg / 20131122]]
( ([TIME[2013-11-23 17:26:28 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20131122>
[29] [CITE@en[JSON-LD 1.0 Processing Algorithms and API]]
( ([TIME[2014-01-13 20:02:37 +09:00]] 版))
<http://www.w3.org/TR/json-ld-api/#the-application-programming-interface>
[30] [CITE@en-GB[ECMAScript Language Specification ECMA-262 6th Edition – DRAFT]]
( ([TIME[2014-01-24 00:08:47 +09:00]] 版))
<https://people.mozilla.org/~jorendorff/es6-draft.html#sec-promise-objects>
[31] [CITE[Promises are now ES6/IDL territory · 3f656c5 · whatwg/dom]]
( ([TIME[2014-02-01 02:22:21 +09:00]] 版))
<https://github.com/whatwg/dom/commit/3f656c52696dd2d90d3796569cc6dc18e73a487b>
[32] [CITE[Promises are now ES6/IDL territory · 3f656c5 · whatwg/dom]]
( ([TIME[2014-04-11 22:59:27 +09:00]] 版))
<https://github.com/whatwg/dom/commit/3f656c52696dd2d90d3796569cc6dc18e73a487b>
[33] [CITE[IRC logs: freenode / #whatwg / 20140513]]
( ([TIME[2014-05-16 00:42:29 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20140513>
[34] [CITE[Copy of Promises Spec Draft as of October 2013]]
( ([TIME[2013-10-23 13:58:10 +09:00]] 版))
<http://www.w3.org/2013/10/json-ld-api/snapshot-promises-draft/>
[35] [CITE[IRC logs: freenode / #whatwg / 20140613]]
( ([TIME[2014-06-14 11:47:11 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20140613#l-594>
[36] [CITE[The "initialization" steps for Web browsers]]
( ([TIME[2014-06-13 01:04:42 +09:00]] 版))
<https://mail.mozilla.org/pipermail/es-discuss/2014-June/037559.html>
[REFS[
- [47] [CITE@en-GB[ECMAScript Language Specification ECMA-262 6th Edition – DRAFT]] ([TIME[2014-08-27 17:42:43 +09:00]] 版) <http://people.mozilla.org/~jorendorff/es6-draft.html#sec-promise-objects>
]REFS]
[39] [[Web IDL]] にも [CODE(IDL)@en[[[Promise<[VAR[T]]>]]]] が追加されました。
[REFS[
- [37] [CITE[Add missing productions for Promise<T> types to the grammar. · 043ad86 · heycam/webidl]] ([TIME[2014-08-03 10:49:57 +09:00]] 版) <https://github.com/heycam/webidl/commit/043ad863465f0b9ec502dc832d708c4cc4e6fc01>
- [38] [CITE[Also allow Promise<void>. · 0a0a5dc · heycam/webidl]] ([TIME[2014-08-03 10:50:07 +09:00]] 版) <https://github.com/heycam/webidl/commit/0a0a5dc2bdada49649cf4b0ca9ece640fe9f15f8>
]REFS]
[40] なお [[Web IDL]] に [CODE(IDL)@en[[[Promise<[VAR[T]]>]]]]
が追加される前に一部の仕様書では [CODE(IDL)@en[[[Promise]]]]
という[[内側型]]なしの構文を使っていました。
;; 現行 [[Web IDL]] 仕様ではこれは認められていません。
[41] [CITE@en[Bug 26517 – Methods that return promises are unable to throw exceptions]]
( ([TIME[2014-08-06 04:40:54 +09:00]] 版))
<https://www.w3.org/Bugs/Public/show_bug.cgi?id=26517>
[42] [CITE[IRC logs: freenode / #whatwg / 20140809]]
( ([TIME[2014-08-10 01:32:41 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20140809>
[45] [CITE@en[Web Applications 1.0 r8804 Integrate with new promise stuff better]]
( ([TIME[2014-09-23 03:28:00 +09:00]] 版))
<https://html5.org/r/8804>
[46] [CITE@en[Quota Management API]]
( ([TIME[2014-09-25 13:32:39 +09:00]] 版))
<https://dvcs.w3.org/hg/quota/raw-file/tip/Overview.html>
[48] [CITE@en[Re: Strawman Promises consensus position, based on Thursday's telechat]]
( ([[Eric Rescorla]] 著, [TIME[2014-10-05 04:01:37 +09:00]] 版))
<http://lists.w3.org/Archives/Public/public-media-capture/2014Oct/0047.html>
[49] [CITE[Intent to implement: Promise.when & Promise.isPromise - Google グループ]]
( ([TIME[2014-10-31 00:55:38 +09:00]] 版))
<https://groups.google.com/forum/#!topic/mozilla.dev.webapi/nW4KoL-6x7A>
[50] [CITE[IRC logs: freenode / #whatwg / 20141030]]
( ([TIME[2014-10-31 00:55:59 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20141030>
[51] [CITE@en[Bug 26939 – Information returned on MediaStreamTrack ended event]]
( ([TIME[2014-10-31 01:09:06 +09:00]] 版))
<https://www.w3.org/Bugs/Public/show_bug.cgi?id=26939>
** 取消可能約束
[52] [[取消可能約束]]参照。
**
[53] [CITE@en[wakaba/perl-promise]]
([TIME[2015-02-27 23:49:34 +09:00]] 版)
<https://github.com/wakaba/perl-promise>
[54] [CITE@en[1152902 – Add a fast path to promises for being resolved with a promise]]
([TIME[2015-04-22 22:36:54 +09:00]] 版)
<https://bugzilla.mozilla.org/show_bug.cgi?id=1152902>
[55] [CITE@en[1156797 – consider exposing a "return value ignored" signal to c++ bindings]]
([TIME[2015-04-22 22:37:31 +09:00]] 版)
<https://bugzilla.mozilla.org/show_bug.cgi?id=1156797>
[56] [CITE@en[1162013 – Microtask scheduled via Promise should run right at the end of the current setTimeout handler even if other setTimeout timers have also expired]]
([TIME[2015-05-07 18:36:34 +09:00]] 版)
<https://bugzilla.mozilla.org/show_bug.cgi?id=1162013>
[60] [CITE@en[Promises are now defined by IDL · whatwg/html@2530b44]]
([TIME[2015-09-02 16:40:40 +09:00]] 版)
<https://github.com/whatwg/html/commit/2530b4400b81d7b7178d22a8202de7ed17a3591a>
[44] [CITE@en[Make HTMLMediaElement.prototype.play() return a promise · whatwg/html@be8edde]]
([TIME[2016-02-02 12:33:13 +09:00]] 版)
<https://github.com/whatwg/html/commit/be8edde20ac46786af968d4de23e90801d4c7213>
[57] [CITE@en[Removed promise antipattern from code example by bergus · Pull Request #227 · whatwg/fetch]]
([TIME[2016-03-10 14:55:09 +09:00]] 版)
<https://github.com/whatwg/fetch/pull/227>
[58] [CITE@en[Merge pull request #113 from domenic/modernize-invoke]]
( ([[bzbarsky]]著, [TIME[2016-05-21 05:55:00 +09:00]]))
<https://github.com/heycam/webidl/commit/09c011d8de1077fe5991ceded97838650e376f6c>
[61] [CITE@en[Issue 627309 - chromium - IDL bindings: Calling a promise-returning method with this=null should reject, not throw - Monorail]]
([TIME[2016-08-11 10:55:23 +09:00]])
<https://bugs.chromium.org/p/chromium/issues/detail?id=627309&desc=2>
[64] [CITE@en[28057 – Support Promise subclass]]
([TIME[2016-10-25 17:00:50 +09:00]])
<https://www.w3.org/Bugs/Public/show_bug.cgi?id=28057>
[65] [CITE@en[Disallow nullable Promise types. (#248)]]
([[tobie]]著, [TIME[2016-12-09 23:20:35 +09:00]])
<https://github.com/heycam/webidl/commit/7366419820e871db6792fd4e7407d490cdaa8020>
[68] [CITE@en[Update attribute setters and getters (#217)]]
([[domenic]]著, [TIME[2017-01-20 17:21:59 +09:00]])
<https://github.com/heycam/webidl/commit/4c9c298c9c37ce5e8da59c2d9becd943703fa06b>
[69] [CITE@en[Fix incorrect '''['''LenientThis''']''' that should be '''['''LenientSetter''']''' (#272)]]
([[domenic]]著, [TIME[2017-01-21 06:21:09 +09:00]])
<https://github.com/heycam/webidl/commit/fef2cdaeb259e79e01b4c2706af1f541e56d4ffe>
[70] [CITE@en[Don't <dfn> Promise. WebIDL already does that. (#1023)]]
([[jyasskin]]著, [TIME[2017-02-24 05:58:13 +09:00]])
<https://github.com/w3c/csswg-drafts/commit/7ff67b227854ab4f37fe0214de41510198f26685>
[77] [CITE@en[Deprecations and removals in Chrome 63 | Web | Google Developers]]
([TIME[2017-10-23 12:40:29 +09:00]])
<https://developers.google.com/web/updates/2017/10/chrome-63-deprecations>
[78] [CITE@en[Promise are part of WebIDL.]]
([[mounirlamouri]]著, [TIME[2017-11-01 18:40:25 +09:00]])
<https://github.com/w3c/screen-orientation/commit/fe3aed325d596c6bd90e27d67d99895013ec8a84>
[79] [CITE@en[27995 – Consider adding some sort of shorthand for resolving a promise with an IDL value]]
([TIME[2017-12-08 16:18:34 +09:00]])
<https://www.w3.org/Bugs/Public/show_bug.cgi?id=27995>
[80] [CITE@en['''['''web-animations-1''']''' Should async playback methods return the `ready` Promise? · Issue #2206 · w3c/csswg-drafts]]
([TIME[2018-06-29 23:03:09 +09:00]])
<https://github.com/w3c/csswg-drafts/issues/2206>