By: Willet in Javascript Tutorials on 2023年04月26日 [フレーム]
promise
A promise is an object in JavaScript that represents the eventual completion or failure of an asynchronous operation, and its resulting value. It allows you to write asynchronous code in a more synchronous way, making it easier to read and reason about. A promise can be in one of three states:
Promises have two main methods: then() and catch(). then() is called when the promise is fulfilled, and takes a callback function with the resolved value as an argument. catch() is called when the promise is rejected, and takes a callback function with the reason for the rejection as an argument.
Here's an example of a promise that returns a resolved value:
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Success!');
}, 1000);
});
promise.then((result) => {
console.log(result); // Output: 'Success!'
});
In this example, the promise resolves after a delay of 1 second, and the then() method is called with the resolved value of 'Success!'.
.then()
In JavaScript, .then() is a method used to handle promises, which are objects representing the eventual completion (or failure) of an asynchronous operation and its resulting value.
The .then() method is used to register a callback function that is called when a promise is resolved, and it takes two arguments: a callback function to be called when the promise is fulfilled (which can also return a promise), and a callback function to be called when the promise is rejected.
Here's an example of how to use .then() with a promise:
const promise = new Promise((resolve, reject) => {
// Do some asynchronous operation
setTimeout(() => {
resolve('Success!');
}, 1000);
});
promise
.then((result) => {
console.log(result); // Output: "Success!"
})
.catch((error) => {
console.error(error); // Output: "An error occurred!"
});
In the example above, we create a new promise that resolves after a timeout of 1 second, then we use .then() to register a callback function that logs the resolved value to the console. If an error occurred, the .catch() method would be called instead.
We use .then() when we want to handle the result of a promise, which is returned asynchronously, and perform some action once the promise is resolved. It's a powerful tool for working with asynchronous code in JavaScript.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in Javascript )
Dynamically modify the option set in Dynamics 365 forms
promise and .then() in JavaScript
reduce() and filter() in JavaScript
History and evolution of Javascript
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Show how many characters remaining in a html text box using javascript
Latest Articles (in Javascript)
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate