Skip to content

Commit c1e9c15

Browse files
committed
add a polyfill for Promise.withResolvers
I'd forgotten to check its compatibility, and it's slightly newer than I'd like. This adds a polyfill at the top of numbas.js, so it can be used on browsers that don't have it built in. The polyfill can be removed once it's widely supported.
1 parent 5194797 commit c1e9c15

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

runtime/scripts/numbas.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ Copyright 2011-14 Newcastle University
2525
_globalThis.Numbas = {}
2626
}
2727

28+
// Polyfill for Promise.withResolvers. Remove once it's reached 95% compatibility.
29+
if(!Promise.withResolvers) {
30+
Promise.withResolvers = function() {
31+
const out = {};
32+
out.promise = new this((resolve, reject) => {
33+
out.resolve = resolve;
34+
out.reject = reject;
35+
});
36+
return out;
37+
}
38+
}
39+
2840
/** @namespace Numbas */
2941
/** Extensions should add objects to this so they can be accessed */
3042
Numbas.extensions = {};

tests/jme-runtime.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ Copyright 2011-14 Newcastle University
2727
_globalThis.Numbas = {}
2828
}
2929

30+
// Polyfill for Promise.withResolvers. Remove once it's reached 95% compatibility.
31+
if(!Promise.withResolvers) {
32+
Promise.withResolvers = function() {
33+
const out = {};
34+
out.promise = new this((resolve, reject) => {
35+
out.resolve = resolve;
36+
out.reject = reject;
37+
});
38+
return out;
39+
}
40+
}
41+
3042
/** @namespace Numbas */
3143
/** Extensions should add objects to this so they can be accessed */
3244
Numbas.extensions = {};

tests/numbas-runtime.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ Copyright 2011-14 Newcastle University
2828
_globalThis.Numbas = {}
2929
}
3030

31+
// Polyfill for Promise.withResolvers. Remove once it's reached 95% compatibility.
32+
if(!Promise.withResolvers) {
33+
Promise.withResolvers = function() {
34+
const out = {};
35+
out.promise = new this((resolve, reject) => {
36+
out.resolve = resolve;
37+
out.reject = reject;
38+
});
39+
return out;
40+
}
41+
}
42+
3143
/** @namespace Numbas */
3244
/** Extensions should add objects to this so they can be accessed */
3345
Numbas.extensions = {};

0 commit comments

Comments
 (0)