0

I am trying to run a simple 2 file node.js project. Project is running a problem and prints an error message "TypeError: steamBroccoli is not a function". I removed the braces from steamBroccoli() - It worked, The project ran without errors. But I don't know how it ran like that and I don't know why it pointed that problem as steamBroccoli is a function.

library.js

let cookBeans = () => { return new Promise ((resolve, reject) => { setTimeout(()=>{ resolve('beans') }, 1000) }) } let steamBroccoli = () => { return new Promise ((resolve, reject) => { setTimeout(()=>{ resolve('broccoli') }, 1000) }) } let cookRice = () => { return new Promise ((resolve, reject) => { setTimeout(()=>{ resolve('rice') }, 1000) }) } let bakeChicken = () => { return new Promise ((resolve, reject) => { setTimeout(()=>{ resolve('chicken') }, 1000) }) } module.exports = {cookBeans, steamBroccoli, cookRice, bakeChicken} 
let {cookBeans, steamBroccoli, cookRice, bakeChicken} = require('./library.js') async function serveDinner(){ const vegetablePromise = steamBroccoli(); // If I remove the braces - The error disappear and the code works. const starchPromise = cookRice(); const proteinPromise = bakeChicken(); const sidePromise = cookBeans(); console.log(`Dinner is served. We're having ${await vegetablePromise}, ${await starchPromise}, ${await proteinPromise}, and ${await sidePromise}.`) } serveDinner(); 
2

1 Answer 1

1

Your example code works! Since you are using arrow functions you can refactor your promises to remove some of the boilerplate code.

const steamBroccoli = () => new Promise((resolve) => { setTimeout(() => resolve('broccoli'), 1000); }); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for offering me the Shorthand.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.