/javascript-export-object-async-quizz

A quiz on how to export an object async way in Javascript

Primary LanguageJavaScript

Javascript Export Object Async Quizz

Introduction

If you export an object as a module, the value of exported object is unchanged regardless of what you do to that object later (async actions). Can we find a way to make value of exported object to be changed?

Quizz

Given we have two files: car.js and index.js

car.js

let car = {};
new Promise((resolve) => setTimeout(resolve, 100)).then(() => {
  car = {
    model: "Tesla",
    color: "black",
    year: "2020",
  };
});
module.exports = car;

index.js

const car = require("./car");
new Promise((resolve) => setTimeout(resolve, 100)).then(() => {
  console.log("My car information:", car); // Should log { model: 'Tesla', color: 'black', year: '2020' }, but it logs {}
});

If you run node index.js, you will see:

$ My car information: {}

Can you modify car.js to make running node index.js to log as following?

$ My car information: { model: 'Tesla', color: 'black', year: '2020' }

Online link

Use can use solve this quizz directly on your browser using this Stackblitz