Skip to content

Commit 11c2fd8

Browse files
committed
More resilient runAff
Detect synchronicity to avoid catching exceptions in handlers.
1 parent 44355f1 commit 11c2fd8

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/Control/Monad/Aff.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,36 @@ exports._attempt = function (Left, Right, aff) {
221221

222222
exports._runAff = function (errorT, successT, aff) {
223223
return function () {
224-
return aff(function (v) {
225-
successT(v)();
224+
var res;
225+
var success;
226+
var status = 0;
227+
var canceler = aff(function (v) {
228+
if (status === 2) {
229+
successT(v)();
230+
} else {
231+
status = 1;
232+
success = true;
233+
res = v;
234+
}
226235
}, function (e) {
227-
errorT(e)();
236+
if (status === 2) {
237+
errorT(e)();
238+
} else {
239+
status = 1;
240+
success = false;
241+
res = e;
242+
}
228243
});
244+
if (status === 1) {
245+
if (success) {
246+
successT(res)();
247+
} else {
248+
errorT(res)();
249+
}
250+
} else {
251+
status = 2;
252+
}
253+
return canceler;
229254
};
230255
};
231256

0 commit comments

Comments
 (0)