const promise = new Promise((resolve, reject) => { setTimeout(() => { resolve("Hello, Promise!"); }, 1000); }); promise.then((result) => { console.log(result); }).catch((err) => { console.log(err); });
function getUserDetails(userId) { return new Promise((resolve, reject) => { if(userId === "123") { resolve({ name: "John", age: 30, email: "john@example.com" }); } else { reject("User not found!"); } }); } getUserDetails("123") .then((user) => { console.log(user); }) .catch((err) => { console.log(err); });This example shows how to create a function that returns a Promise. In this case, the Promise resolves with the user details object if the userId matches "123", and rejects with an error message if it doesn't. The then() and catch() methods are used to handle the resolved value and any errors that may have occurred. Package Library: RSVP.js In conclusion, RSVP.js is a popular package library for handling promises in JavaScript. It provides a useful set of utility methods and functions for creating, handling, and managing promises in a simple and efficient way.