ES6 - Create a JavaScript Promise

Hi There quick question is there a particular reason that this code

const makeServerRequest = new Promise ((resolve,reject) => {

does not require the return keyword, considering the the first character after the arrow is an opening curly brace?

I think this example in the instructions is just showing you the general syntax. This isn’t meant to be a complete function. In the real world you would have code inside of the body (which you will see in the next challenge).

Also, a return statement is required to return a value here. But what does a function return if there is no return statement? So even without an explicit return statement it is still returning a value.

i didnt quite understand but i think since the next challange had the resolve / reject returns it returns those ? and if not it must return undefined?

thank you

When dealing with arrow functions in JavaScript, the return keyword behaves in the following manner:

  1. If the arrow function’s body consists of a single expression, you have the option to omit both the braces and the return keyword. The expression’s result will be automatically returned.

However, if the body of the arrow function is enclosed within braces ({}), it is treated as a code block. This block can contain multiple statements, and if you wish to return a value, you must employ the return keyword.

In the specific case of the arrow function passed to new Promise in your code, it is unnecessary for it to return anything. The Promise constructor does not require the return value from this function. Instead, the function is expected to eventually invoke either resolve or reject, which will determine the promise’s outcome.

As a result, there is no need for your code to include a return statement.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.