This project includes utility functions to handle asynchronous operations, detect Node.js environments, and customize the behavior of console logs. Below is a description of each function and how to use them.
This function handles asynchronous operations with a try-catch
mechanism, passing success
and error
parameters to the provided callback.
pcall(async (success, error) => {
if (success) {
console.log("Operation completed successfully.");
} else {
console.error("Operation failed:", error);
}
});
-
Parameters:
callback(success, error)
success
: Boolean indicating if the operation succeeded.error
: The error object (if any) that occurred during the operation.
-
Returns: None (callback executed with
success
anderror
).
This function checks if the environment is Node.js and executes the callback only in Node.js. If not, it logs a message indicating that Node.js is not detected.
pcallNode((success, error) => {
if (success) {
console.log("Running in Node.js environment.");
} else {
console.error("Error detected in Node.js environment:", error);
}
});
-
Parameters:
callback(success, error)
success
: Boolean indicating if the environment is Node.js.error
: Error object if something goes wrong inside the callback.
-
Returns: None (callback executed with
success
anderror
).
This function customizes the behavior of console.log
, console.warn
, and console.error
to display styled logs on a webpage. It overrides the default console
methods to log messages in a specific format on the page.
createLogs();
console.log("This is a log message.");
console.warn("This is a warning message.");
console.error("This is an error message.");
-
Parameters: None
-
Returns: None (modifies the behavior of console logs).
-
Description:
console.log
: Displays messages in a light grey background.console.warn
: Displays warning messages in an orange background.console.error
: Displays error messages in a red background.
These messages are inserted into the document body in styled div elements.
There’s no installation required for this script. Simply include it in your HTML or JavaScript file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Utility Functions</title>
</head>
<body>
<script src="path/to/your/script.js"></script>
<script>
createLogs();
console.log("This is a log message.");
console.warn("This is a warning message.");
console.error("This is an error message.");
</script>
</body>
</html>