Skip to content
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

Global Promise assumed from custom? See resultant awaiter code. #12374

Closed
electricessence opened this issue Nov 19, 2016 · 5 comments
Closed
Labels
Question An issue which isn't directly actionable in code

Comments

@electricessence
Copy link

electricessence commented Nov 19, 2016

After updating to the dev version. I noticed that the diff when I checked in showed this code with Promise_1.Promise instead of void 0. But then when I thought about it, how is this happening?
The compiler tells me I'm not allowed to use Promise as a name, but then somehow knows where to get my own Promise module from? :/

Can someone please explain what's going on here? Why is this changing so much? What is this magic?

TypeScript Version: 2.2.0-dev.20161117

Code
https://github.com/electricessence/TypeScript.NET/blob/master/_gulp/bumpVersion.js#L22
https://github.com/electricessence/TypeScript.NET/blob/master/_gulp/bumpVersion.ts#L27

My solution for 2.1.1 was to create an awaiter with a factory so that each module could actually have it's own Promise lib:
https://github.com/electricessence/TypeScript.NET/blob/master/source/awaiter.ts#L50

@aluanhaddad
Copy link
Contributor

It looks to me like the transpiled code doesn't use the import alias because it doesn't need to. Promise_1.Promise is what you have aliased as NPromise. It uses the value of this import as the constructor function to past to __awaiter. That seems consistent.

@electricessence
Copy link
Author

@aluanhaddad It makes no sense to me that it's automatically figuring out what my promise module is.
I did nothing to instruct it what to use. It's very automagicky. And it's different than what I saw in 2.1.1.
Take note of what's really going on here. In 2.1.1 it simply passed void 0 instead of Promise_1.Promise.

@electricessence
Copy link
Author

Also I found this odd:

export function awaiterFactory(UserPromise:PromiseConstructorLike):Awaiter
{
    return (thisArg:any, _arguments:any[], P:PromiseConstructorLike, generator:Function) =>
    {
        awaiter(thisArg, _arguments, P || UserPromise, generator);
    };
}

I know it's my code, but I expected it to work to fix my woes.
What I discovered is that the underlying code relies on context so that if I use a factory method like this, I get very strange results from certain operations. So the good news is. The weird magic above fixes my issues (with what I'm currently doing).

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Nov 19, 2016

It makes no sense to me that it's automatically figuring out what my promise module is.
I did nothing to instruct it what to use.

in the TypeScript source you have

async function bumpVersion(type:string): NPromise<File[]> {
}

Here, the compiler uses the return type NPromise<T> to figure out what Promise value to use.
Hence the emitted JavaScript is

function bumpVersion(type) {  // Promise_1.Promise === NPromise
  return __awaiter(this, void 0, Promise_1.Promise, function () {
  } 
}

This is the only case, as far as I know, of type directed emit in TypeScript and was necessary to downlevel async/await to ES3/ES5. It is not automagical, just irregular with respect to the reset of the language which does not otherwise alter emit based on type annotations.

@electricessence
Copy link
Author

@aluanhaddad OHHH WOW. Okay... That's really smart. :|
I didn't even think of that. Very cool. Thank you.

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Nov 21, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants