Skip to content

PCall.js is a Try-Catch for JavaScript using pcall in github.

Notifications You must be signed in to change notification settings

SkunkPlatform-Team/pcall.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 

Repository files navigation

JavaScript Utility Functions

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.

Functions

1. pcall(callback)

This function handles asynchronous operations with a try-catch mechanism, passing success and error parameters to the provided callback.

Usage Example:

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 and error).


2. pcallNode(callback)

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.

Usage Example:

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 and error).


3. createLogs()

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.

Usage Example:

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.


Installation

There’s no installation required for this script. Simply include it in your HTML or JavaScript file.

Example Usage in HTML:

<!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>

About

PCall.js is a Try-Catch for JavaScript using pcall in github.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published