Promise.gml
Link to Promise.gml
Promise.gml
An adaptation of JavaScript Promises for GameMaker Studio 2.3+, based on this polyfill.
JS➜GML Equivalents
GameMaker does not allow using built-in function names as variable names, so:
- new Promise ➜ new Promise
- promise.then ➜ promise.andThen
- promise.catch ➜ promise.andCatch
- promise.finally ➜ promise.andFinally
- Promise.all ➜ Promise.afterAll
- Promise.allSettled ➜ Promise.allSettled
- Promise.any ➜ Promise.any
- Promise.race ➜ Promise.race
- Promise.reject ➜ Promise.reject
- Promise.resolve ➜ Promise.resolve
Changes
GameMaker does not allow naming methods same as keywords, therefore:
then
➜andThen
catch
➜andCatch
finally
➜andFinally
all
➜afterAll
Examples
Can also be found in the sample project, along with supporting scripts.
Basic (ft. custom setTimeout):
(new Promise(function(done, fail) {
setTimeout(function(_done, _fail) {
if (random(2) >= 1) _done("hello!"); else _fail("bye!");
}, 250, done, fail);
})).andThen(function(_val) {
trace("resolved!", _val);
}, function(_val) {
trace("failed!", _val);
})
afterAll:
Promise.afterAll([
Promise.resolve(3),
42,
new Promise(function(resolve, reject) {
setTimeout(resolve, 100, "foo");
})
]).andThen(function(values) {
trace(values);
});
Chaining HTTP requests (ft. custom HTTP wrappers):
http_get_promise("https://yal.cc/ping").andThen(function(v) {
trace("success", v);
return http_get_promise("https://yal.cc/ping");
}).andThen(function(v) {
trace("success2", v);
}).andCatch(function(e) {
trace("failed", e);
})
Caveats
- Non-exact naming (but feel free to pick your own aliases).
- Have to "promisify" built-in functions to be able to finely use them with promises.
- I could not port the original JS library's unit tests because their dependencies have far more code than the library itself.