Home > Blockchain >  Can Javascript Promises be created with a single parameter?
Can Javascript Promises be created with a single parameter?

Time:10-19

When we create a Promise(), the constructor expects two parameters: resolve and reject.
Can I create a Promise using only resolve?

let promise = new Promise((resolve) => {});

I've tried and it works. But, it is a good practice? The promise is expected to be resolved in all circumstances.

CodePudding user response:

Yes, it's doable. If

  • you're sure the Promise will always resolve, or
  • there is no real way to detect if an error occurs (in which case a reject parameter wouldn't be used anyway)

then using only the resolve parameter is a reasonable possibility.

If there's any chance of an error - even an unexpected error - it'd be a good idea to use the reject parameter, though. Many errors are not expected, after all.

CodePudding user response:

yes you can with

Promise.resolve(value);

refer https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve

  • Related