Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
114
rated 0 times [  121] [ 7]  / answers: 1 / hits: 20889  / 3 Years ago, sat, january 16, 2021, 12:00:00

Function fetchData returns a promise and then I'm dealing with that promise in generateURL function by chaining the promise with .then however it returns Promise < pending >. Function generateURL should return a string What am I doing wrong?


const fetch = require('node-fetch');

const fetchData = async () => {
return await fetch('https://jsonplaceholder.typicode.com/todos/1');
};

const generateURL = () => {
const baseURL = 'https://cdn.test.com/';
fetchData().then((res) => {
const data = res.json();
console.log('data', data);
const id = data.id;
console.log('id', id);
const generatedURL = `${baseURL}${id}`;
return generatedURL;
});
};


More From » node.js

 Answers
30

Reduced your code by a bit instead of adding multiple promise resolution


  const fetch = require('node-fetch');

const everything = async () =>{
const response = await fetch('https://jsonplaceholder.typicode.com/todos/1');
const {id} = await response.json();
return `https://cdn.test.com/${id}`
}
everything().then((res)=>console.log(res));

[#50444] Friday, January 1, 2021, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
editha

Total Points: 564
Total Questions: 107
Total Answers: 109

Location: Finland
Member since Fri, Oct 21, 2022
2 Years ago
;