-
-
Notifications
You must be signed in to change notification settings - Fork 184
Add support for Promises #1049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Promises #1049
Conversation
|
Woops, putting this back into draft after realising I forgot the case where |
Problem solved. |
# Conflicts: # src/LuaLib.ts # src/lualib/declarations/global.d.ts
tomblind
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing I don't see represented in the tests is direct chaining:
promise.then(...).then(...).catch(...)
It might be good to test this to ensure these calls are returning the correct thing.
test/unit/builtins/promise.spec.ts
Outdated
|
|
||
| util.testFunction` | ||
| // Wrap function getUserData(id: number, callback: (userData: UserData) => void) into a Promise | ||
| async function getUserDataAsync(id: number): Promise<UserData> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dumb question: how does this work if we don't support async yet?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works because async keyword has no special function yet and will just be ignored. You are right that this is a a strange async, and in fact I think it is removed again in my async/await branch. I will remove it here too.
| ` | ||
| .setTsHeader(promiseTestLib) | ||
| .expectToEqual(["finally"]); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe another test for finally that checks that this:
myPromise
.then(response => {
doSomething(response);
runFinalCode();
})
.catch(e => {
returnError(e);
runFinalCode();
});is equivalent to this:
myPromise
.then(response => {
doSomething(response);
})
.catch(e => {
returnError(e);
})
.finally(() => {
runFinalCode();
});Can possibly be implemented like this:
const { noThrow, noThrowResolve } = defer<string>();
const { throw_, throwResolve } = defer<string>();
noThrow.then(
data => { log(data); log("finally"); }
)
.catch(
e => { log(e); log("finally"); }
);
throw_.then(
data => { throw data; log("finally"); }
)
.catch(
e => { log(e); log("finally"); }
);
noThrowResolve("test");
throwResolve("test");
const { finallyNoThrow, finallyNoThrowResolve } = defer<string>();
const { finallyThrow, finallyThrowResolve } = defer<string>();
finallyNoThrow.then(
data => { log(data); }
)
.catch(
e => { log(e); }
)
.finally(
() => { log("finally"); }
);
finallyThrow.then(
data => { throw data; }
)
.catch(
e => { log(e); }
)
.finally(
() => { log("finally"); }
);
finallyNoThrow("test");
finallyThrow("test");
return allLogs;
// expectToEqual(["test", "finally", "test", "finally", "test", "finally", "test", "finally"])* Initial lualib promise class implementation * First promise tests * More promise tests * Promise class implementation * Implemented Promise.all * Promise.any * Promise.race * Promise.allSettled * fix prettier * Add promise example usage test * Added missing lualib dependencies for PromiseConstructor functions * Immediately call then/catch/finally callbacks on promises that are already resolved * Transform all references to Promise to __TS__Promise * PR feedback * Removed incorrect asyncs * Add test for direct chaining * Add test for finally and correct wrong behavior it caught * Added test throwing in parallel and chained then onFulfilleds * Fixed pull request link in ArrayIsArray lualib comment
Out of scope: async/await, will be done in follow-up PR
Implemented promises based on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise and a little bit of https://promisesaplus.com/
Note: For now we are deviating from the spec in an important aspect: currently the promise constructor function is executed immediately, instead of waiting until there is no more user code on stack, like the promise spec mandates.