Promise

by REPLY2SAGAR

JavaScript

function readFilePromise(filePath) {
    return new Promise((resolve, reject) => {
        setTimeout(() => { // Simulate a file read delay
            if (filePath === "error.txt") {
                reject("Failed to read file");
            } else {
                const fileResult = "File content of " + filePath;
                resolve(fileResult);
            }
        }, 1000);
    });
}

readFilePromise("example.txt")
    .then(content => {
        console.log("Received file content:", content);
    })
    .catch(error => {
        console.error("Error reading file:", error);
    });