Simulate import() with a promise and use its export.
Acceptance criteria
- 1.#out should show 10.
JavaScript Tutorial · JavaScript Modules
These 10 tasks go with the JavaScript Dynamic Import chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “import() loads a module while the program is running, returning a promise.” .
1.Load a module dynamicallyexercise · easy · 10 min
Simulate import() with a promise and use its export.
Passes when: #out should show 10.
2.Await a dynamic importexercise · easy · 10 min · practice plan
Use async and await to load a simulated module.
Passes when: #out should show 42.
3.Load conditionallyexercise · easy · 10 min · practice plan
Only load the module when it is actually needed.
Passes when: #out should show chart rendered.
4.Handle a failed importexercise · easy · 10 min · practice plan
Catch a rejected dynamic import and report the message.
Passes when: #out should show module not found.
5.Destructure the resolved moduleexercise · medium · 10 min · practice plan
Pull two exports straight out of the resolved value.
Passes when: #out should show 16 and 28.
6.Fix the module used without waitingbug fix · easy · 8 min
The result is a promise until it resolves.
Passes when: #out should show 10.
7.Fix await missing inside the async functionbug fix · easy · 8 min · practice plan
Without await, math is a promise, not the module.
Passes when: #out should show 42.
8.Fix the module loaded even when not neededbug fix · easy · 8 min · practice plan
The module should only load when the condition is actually true.
Passes when: #out should show never loaded.
9.Fix the error message discardedbug fix · easy · 8 min · practice plan
The catch block should report the real error, not a fixed message.
Passes when: #out should show module not found.
10.Fix the destructuring done too earlybug fix · easy · 8 min · practice plan
The module has not resolved yet when the names are pulled out.
Passes when: #out should show 16 and 28.
Between them these tasks cover import(), Promises and Conditional loading.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.