Last active
March 19, 2019 21:10
-
-
Save leobalter/998d14c67e807b45c81a6dbaf77feda2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 */ }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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