undefinedfix
Sign in

Asynchronous loading and output of ES6 module

jlhfz edited in Sat, 03 Oct 2020

Ask the great God a question. Well, there is a JS file, and then there is an asynchronous Ajax request in it. You need to assign the result of the request to a variable to export, and use export. However, because the Ajax request is asynchronous, the variable has no value when exporting. If you write then () in promise to export, it will not work, because export must be at the top level of the file. How does this kind of situation solve excuse me?? Urgent, this problem has been bothering us for a long time.

2 Replies
suxbr
commented on Sat, 03 Oct 2020

Export a promise object directly

export let something = new Promise(resolve => {
    //省略
});

Use then to get after import

import {something} from 'module';
something.then(i =>{
    //省略
});
Microtrend
commented on Sat, 03 Oct 2020

But it's not a good idea

The promise of ES6 is to let you return. You can call then outside

lock This question has been locked and the reply function has been disabled.