A better alternative to "new Promise()" that supports error handling and maintains the stack trace for Error.stack.
When you use new Promise() you need to wrap your code inside a try-catch to call reject on error.
asyncOperation() does this automatically.
When you use new Promise() you will lose the stack trace when reject(new Error()) is called.
asyncOperation() makes sure the error stack trace is the correct one.
Example
awaitasyncOperation(async (resolve, reject) => { awaitdoSomething(); //if this fails, reject() will automatically be called stream.on('data', (data) => { resolve(data); //at some point you MUST call resolve(data) }); });
A better alternative to "new Promise()" that supports error handling and maintains the stack trace for Error.stack.
When you use
new Promise()
you need to wrap your code inside a try-catch to callreject
on error. asyncOperation() does this automatically.When you use
new Promise()
you will lose the stack trace whenreject(new Error())
is called. asyncOperation() makes sure the error stack trace is the correct one.Example