Unhandledpromiserejectionwarning Error Invalid Continuation Byte Solidity

What causes unhandled promise rejection warningThe unhandledpromiserejectionwarning error in NodeJS usually takes place in async await functions. It is a common error that most people confuse with something super complex in their NodeJS code. In this post, you will learn about the source of this error and how you can solve it

Contents

  • Why Am I Getting Unhandledpromiserejectionwarning
    • – Understanding Promises
    • – Example With Chain of Promises
    • – Example With Not Handling Rejection in Promise
    • – Example With Validation Error
  • How To Solve This Error
    • – Example With .Catch()
    • – Example With Try/Catch Block
    • – Solving the Error in a Chain of Promises
    • – Using the Unhandledrejection Event
  • FAQ Section
    • – Why Am I Getting the Unhandledpromiserejection With Await?
    • – How Can I Handle This Warning in NodeJS?
    • – What Is the Outcome of Putting a Rejected Promise in Await?
    • – Can You Use Await in a Promise?
    • – Why Use Promises?
    • – Can I Chain Promises?
    • – Can Async Executor Become Promise?
    • – What Makes Async Await Superior To Promises
  • Conclusion

Why Am I Getting Unhandledpromiserejectionwarning

There is one obvious reason that could be causing this warning. In NodeJS, the error arises when you add an asynchronous function that lacks a catch block. For operations that do not clear immediately, JavaScript shows asynchronous behavior. Such operations accept callbacks, which are functions executed after the completion of an operation.

Callbacks in JavaScript can be very frustrating. Sometimes they may make the code difficult to comprehend or even unreadable. Fortunately, NodeJS has Promises. Usually, a Promise object indicates the eventual failure or completion of an asynchronous operation. Provided a function supports a callback, you can wrap it using the fulfillment handler .then() for retrieving returned data or carry on with other operations like so:

const slow = new Promise(function(resolve, reject) {
setTimeout(function() {
console.log('Slow but sure.');
resolve();
}, 500);
});
slow
.then(function() {
console.log('So the last shall be first, and the first last');
});

As well, you can use Async/Await – syntax for Promises to make Javascript A lot easier and simpler to read. Here is an example of how you can use this syntax:

const slow = new Promise(function(resolve, reject) {
setTimeout(function() {
console.log('Slow but sure.');
resolve();
}, 500);
});
(async function(){
await slow;
console.log('So the last shall be first, and the first last');
})();

The problem comes in when you forget to deal with rejections and it can result in issues that are hard to debug. So, if you forget to deal with rejections, you will get this error warning in NodeJS.

– Understanding Promises

A Promise denotes a machine representation of asynchronous operation. The representation can be in one of three states – pending, fulfilled, or rejected. Usually, a pending promise indicates an operation that is still in progress while a fulfilled promise represents an operation that executes successfully. However, a rejected promise indicates that for some reason an operation failed.

– Example With Chain of Promises

However, it is not always this easy. If you have several Promises, things tend to be more interesting. Below is an example of how you can trigger this warning with several chains of Promises.

const slow1 = new Promise(function(resolve, reject) {
throw new Error('What is happening :(');
});
const slow2 = new Promise(function(resolve, reject) {
slow1.then();
});
(async function(){
try {
await slow2;}
catch(er){
console.log(er);
}
})();

Even though you wrap slow2 in a try/catch, it does not cover all the Promises. So, you need to handle the exception properly. This is the reason rejections that are unhandled tend to be very insidious. Sometimes you may think you have handled an error but the error handler may cause another error.

Additionally, you get the same problem when you return a promise in the .catch() function.

– Example With Not Handling Rejection in Promise

If you run the code below, you will get the unhandled rejection error.

const slow = new Promise(function(resolve, reject) {
reject()
});
(async function(){
await slow;
})();

– Example With Validation Error

Note that you will get this warning if a validation error is thrown in the Promise like so:

const slow = new Promise(function(resolve, reject) {
throw new Error('What is happening :(');
});
(async function(){
await slow;
})();

The output, in this case, will be as follows:

(node:18874) UnhandledPromiseRejectionWarning: Error: What is happening 🙁
at index.js:2:11
at new Promise (<anonymous>)
at Object.<anonymous> (index.js:1:14)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
at internal/main/run_main_module.js:17:11
(node:18874) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:18874) [DEP0018] DeprecationWarning: Unhandled rejections are deprecated. In the future, rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Please take notice of the warning that includes a deprecation warning. It means that in the future if you fail to handle promises correctly, the error can crash your application.

How To Solve This Error

The easiest way of handling promise rejections entails the use of .catch() failure handler or a try/catch block. Below is a quick look at the syntax that lets you fix the error when it happens in async await functions.

const nameofFunction = async (arguments) {
try {
// Insert code
} catch (error) {
// Deal with rejections here
}
};

– Example With .Catch()

To suppress the warning, you only need to use .catch() on the Promise and implement an empty function.

const slow = new Promise(function(resolve, reject) {
reject();
});
slow. then(function() {
console.log('So the last shall be first, and the first last')
})
.catch(function(er){
console.log('The error: ', er);
})

The output will be:

– Example With Try/Catch Block

Below is a short example of how you can use try/catch block to handle rejections.

const slow = new Promise(function(resolve, reject) {
throw new Error('What is happening :(');
});
(async function(){
try {
await slow;}
catch(er){
console.log(er);
}
})();

– Solving the Error in a Chain of Promises

If you recall, wrapping one function in a try/catch where you have multiple chains of Promises will not solve the issue for all the Promises. Thus, you need to come up with a proper way of handling rejections in promises.

You can solve the issue in the example of multiple Promises like so:

const slow1 = new Promise(function(resolve, reject) {
throw new Error('What is happening :(');
});
const slow2 = new Promise(function(resolve, reject) {
slow1.then().catch(function() {reject();});
});
(async function(){
try {
await slow2;}
catch(er){
console.log('The error is: ', er);
}
})();

However, you may not always anticipate where error handling is necessary, especially when using third-party libraries. There are so many surprises with rejections that are unhandled that NodeJS provides you with a mechanism for globally handling these rejections.

– Using the Unhandledrejection Event

The global process node includes an unhandledRejection event that helps you take care of rejections that are unhandled. Note that although the parameter to the event needs to be a JavaScript error, it is not a must. The following code will help you catch every unhandled Promise rejection.

process.on('unhandledRejection', function(er) {
console.log(er);
});

Here is a quick example of how you can put the above code to use in dealing with unhandled Promise rejections.

process.on('unhandledRejection', function(er) {
console.log(er);
});
const slow1 = new Promise(function(resolve, reject) {
throw new Error('What is happening :(');
});
const slow2 = new Promise(function(resolve, reject) {
slow1.then();
});
(async function(){
try {
await slow2;}
catch(er){
console.log(er);
}
})();

FAQ Section

– Why Am I Getting the Unhandledpromiserejection With Await?

You will get this error when you throw a promise inside the async function without using a catch block. Also, NodeJS will generate this error when it rejects a promise that you did not handle using .catch().

– How Can I Handle This Warning in NodeJS?

You can handle this error in several ways. First, you can call on the try/catch block, or .catch() failure handler. Second, you can choose to use the unhandledrejection event handler to accomplish this.

– What Is the Outcome of Putting a Rejected Promise in Await?

If a promise is rejected, putting it in await expression generates the rejected values. In case the value after the await operator turns out to be not a promise, the value is transformed into a resolved promise. The await operator splits the flow of execution, which allows async function call to continue executing.

– Can You Use Await in a Promise?

You can use the await keyword in the async function before a call to a promise returning function. This allows the code to wait until the promise settles resulting in either the fulfilled promise value becoming a return value or throwing the rejected value.

– Why Use Promises?

With Promises, you get a perfect choice for dealing with asynchronous operations in a very simple way. Besides, you can deal with multiple asynchronous operations with ease. What's more, they offer superior error handling compared to events and callbacks.

– Can I Chain Promises?

Yes. You can accomplish this when a callback function yields a promise. It lets you chain the promise to another promise that in return will run after fulfilling the second promise. You can use .catch() to handle errors that may arise along the way.

– Can Async Executor Become Promise?

Yes. An executor function can be a promise. However, it is usually a mistake for several reasons. First, if the executor function generates an error, it won't cause a rejection of the newly-built Promise. Also, it may lead to difficulties debugging and handling some errors.

– What Makes Async Await Superior To Promises

First, it is a wrapper for restyling code and makes promises easier to use and read. It paves the way for making asynchronous code appear more procedural/synchronous and easier to comprehend. You can only use await in async functions.

Conclusion

Whenever this error pops up, there are a few things you need to look out for. These include:

  • Promises with rejections that are unhandled
  • Asynchronous function without an error handling block
  • The solution involves the use of either a .catch() failure handler or a try/catch block
  • Also, you can call upon the Unhandledrejection event

Unhandled promise rejection warningThat is how you can pinpoint and solve this error in NodeJS. Give it a try.

  • Author
  • Recent Posts

Position is Everything

deitchwharry.blogspot.com

Source: https://www.positioniseverything.net/unhandledpromiserejectionwarning

0 Response to "Unhandledpromiserejectionwarning Error Invalid Continuation Byte Solidity"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel