Skip to content

Instantly share code, notes, and snippets.

@leobalter
Last active March 19, 2019 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leobalter/998d14c67e807b45c81a6dbaf77feda2 to your computer and use it in GitHub Desktop.
Save leobalter/998d14c67e807b45c81a6dbaf77feda2 to your computer and use it in GitHub Desktop.
// Status of today + dynamic import() proposal, await unwraps namespace objects.
//--- a.js
export let then = async () => {/* code */};
//--- b.js
import * as ns from 'a.js';
async function fn() {
await Promise.resolve(ns); // should unwrap the "thenable" namespace
return ns;
}
fn().then(val => { /* val is the returned value from a.js' `then` */ };
//--- c.js
async function fn() {
return await import('a.js'); // should unwrap the "thenable" namespace
// problem, no access to the actual ns object await import might resolve to any JS completion.
}
fn().then(wat => { /* what do we have here? Not a ns, but the unwrapped return of a.js' `then` */ });
import('a.js').then(wat => { /* this is wacky, this will be something else than the namespace */ });
// Possible solution: Promises do not unwrap today "thenable" namespaces
//--- a.js
export let then = async () => {/* code */};
//--- b.js
import * as ns from 'a.js';
async function fn() {
await Promise.resolve(ns); // should return the ns
return ns;
}
fn().then(val => {
val === ns // true
val.then === ns.then // true
};
//--- c.js
async function fn() {
await import('a.js'); // should evaluate to the namespace object
await await import('a.js'); // should also evaluate to the namespace object
await { ... await import('a.js') }; // finally unwraps the thenable object
(await import('a.js')).then // === the `then` function from a.js
}
fn();
// Other solutions? Trying bindings
//--- a.js
export default 42;
export let then = async () => {/* code */};
export let foo = 1;
//--- b.js
async function fn() {
// Possible change to import(), wrap the binding names, seems overkill:
await import('a.js'); // { ns: { default: 42, then: _function_, foo: 1 } }
// Here `ns` is the actual namespace object.
await (await import('a.js')).ns; // This is trully intentional
await import('a.js').then(({ns}) => ns); // same here
}
fn();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment