J. Doe
J. Doe

Reputation: 309

How to import promised based variable to use in another module (javascript)?

how to import a value of the promised based function to another module?

// index.js file:

import test from "./test";
console.log(test) // expected result should be "delectus aut autem" - now getting Promise object


// test.js file:

var test = fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(response => response.json())
  .then(function (data) {
    return data.title
  });

export default test

working example: https://js-njm471.stackblitz.io

Upvotes: 0

Views: 100

Answers (1)

Nick
Nick

Reputation: 16586

Since test is a promise, you simply have to handle it as such after you import it.

test.then(data => { 
    console.log(data);
});

Upvotes: 1

Related Questions