return resolve() simply end the function as a normal return , which simply depends on the flow of your code. If you do not want or do not need any code to execute your function, use return to exit the function
return new Promise( (resolve, reject) => { fs.readFile(file, (err, data) => { if (err) reject(err) return resolve(data) console.log('after return') // won't execute }) })
only resolve will create a successful promise state, but will execute code execution, if any, when return not used.
Remember to resolve() and reject() to create a promise state, they cannot be changed after creating the state, .then and .catch are used for further execution, the use of return depends entirely on your code stream. If you do not want to execute more code in this block, then return resolve()
return new Promise( (resolve, reject) => { fs.readFile(file, (err, data) => { if (err) reject(err) resolve(data) console.log('after return') // will execute }) })
it is just like the normal return in function and has nothing to do with the promise
p0k8_
source share