Skip to content

Commit 87c7638

Browse files
committed
Q.promise() - Support executor
1 parent 35dc158 commit 87c7638

File tree

4 files changed

+87
-7
lines changed

4 files changed

+87
-7
lines changed

qml/QuickPromise/Q.qml

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ QtObject {
1515
QPTimer.clearTimeout(id);
1616
}
1717

18-
function promise() {
19-
return PromiseJS.promise();
18+
function promise(executor) {
19+
return PromiseJS.promise(executor);
2020
}
2121

2222
function resolved(result) {

qml/QuickPromise/promise.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/* JavaScript implementation of promise object
88
*/
99

10-
function Promise() {
10+
function Promise(executor) {
1111

1212
this.state = "pending";
1313

@@ -22,6 +22,21 @@ function Promise() {
2222
this.isSettled = false;
2323
this.isFulfilled = false;
2424
this.isRejected = false;
25+
26+
if (typeof executor === "function") {
27+
var promise = this;
28+
29+
try {
30+
executor(function() {
31+
promise.resolve.apply(promise, arguments);
32+
}, function() {
33+
promise.reject.apply(promise, arguments);
34+
});
35+
36+
} catch(e) {
37+
promise.reject(e);
38+
}
39+
}
2540
}
2641

2742
function instanceOfPromiseJS(object) {
@@ -89,8 +104,9 @@ Promise.prototype.then = function(onFulfilled,onRejected) {
89104

90105

91106
Promise.prototype.resolve = function(value) {
92-
if (this.state !== "pending")
107+
if (this.state !== "pending") {
93108
return;
109+
}
94110

95111
if (this === value) { // 2.3.1
96112
this.reject(new TypeError("Promise.resolve(value) : The value can not be same as the promise object."));
@@ -252,6 +268,6 @@ Promise.prototype._setState = function(state) {
252268
}
253269

254270

255-
function promise() {
256-
return new Promise();
271+
function promise(executor) {
272+
return new Promise(executor);
257273
}
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import QtQuick 2.0
2+
import QtTest 1.0
3+
import QuickPromise 1.0
4+
5+
6+
TestCase {
7+
name : "PromiseJS_Executor"
8+
9+
function tick() {
10+
wait(0);
11+
wait(0);
12+
wait(0);
13+
}
14+
15+
function test_promise_executor_resolve() {
16+
var promise = Q.promise(function(fulfill, reject) {
17+
fulfill(123);
18+
});
19+
compare(promise.isFulfilled, false);
20+
compare(promise.isRejected, false);
21+
22+
tick();
23+
24+
compare(promise.isFulfilled, true);
25+
compare(promise.isRejected, false);
26+
compare(promise._result, 123);
27+
28+
}
29+
30+
function test_promise_executor_reject() {
31+
var promise = Q.promise(function(fulfill, reject) {
32+
reject(456);
33+
});
34+
compare(promise.isFulfilled, false);
35+
compare(promise.isRejected, false);
36+
37+
tick();
38+
39+
compare(promise.isFulfilled, false);
40+
compare(promise.isRejected, true);
41+
compare(promise._result, 456);
42+
43+
}
44+
45+
46+
function test_promise_executor_exception() {
47+
var promise = Q.promise(function(fulfill, reject) {
48+
throw("error");
49+
});
50+
compare(promise.isFulfilled, false);
51+
compare(promise.isRejected, false);
52+
53+
tick();
54+
55+
compare(promise.isFulfilled, false);
56+
compare(promise.isRejected, true);
57+
compare(promise._result, "error");
58+
59+
}
60+
61+
}
62+
63+

tests/unittests/unittests.pro

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ DISTFILES += \
2828
tst_promise_resolvewhen_all_promise.qml \
2929
tst_promisejs_resolve_qt_binding.qml \
3030
tst_timer.qml \
31-
tst_promisejs_examples.qml
31+
tst_promisejs_examples.qml \
32+
tst_promisejs_executor.qml

0 commit comments

Comments
 (0)