In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "what is promise and how to use it". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is promise and how to use it".
Js is single-threaded, and Promise solution is an idea of asynchronous programming, which is much more powerful than traditional callback functions and events. Promise is the placeholder for the result of an asynchronous operation. Instead of subscribing to an event or passing a callback function to the target function, the function returns a Promise (equivalent to the order number).
Promise: commitment in English means that the Promise object holds the result of an event (usually an asynchronous operation) that ends in the future.
Promise has three states: (recommended study: Javascript video tutorial)
Pendding,rejected,resolved
There are only two possibilities for switching the state of a Promise object:
Pendding- > rejected,pendding- > resolved
Basic usage:
New Promise ((resolved,rejected) = > {})
The resolved,rejected function of the Promise object. When the callback of the asynchronous event status pendding- > resolved succeeds, the resolved function is called; when the asynchronous operation fails, the rejected function is called back.
Promise's then (resolved,rejected) function has two arguments, a resolved function and a rejected function.
Promise's catch (): catch promise error function, just like rejected in then function parameters, handle errors. Because Promise throws errors with bubbling nature, can be passed continuously, and will be passed to catch, so it is recommended that all error handling be placed in catch, and only successful ones are handled in then.
A major feature of Promise is that it can be called chained, and the Promise object can be returned in then,catch.
Promise.all ([promise1,promise2]): the parameter is an array of promise objects. When the state of all promise objects is resolved, the state of the object will be resolved and then will be called immediately. When a promise object is rejected, the state of the object will be rejectd and catch will be executed.
Promise.race ([promise1,promise2]): racing function. When the state of a promise object changes, the object takes the same state and executes the corresponding function.
Life cycle of promise
Each Promise goes through a short life cycle, initially in a pending state state, indicating that the asynchronous operation is not yet over. A pending Promise is also considered to be unsettled. Once the asynchronous operation ends, the Promise is considered settled and enters one of two possible states:
1. Completed (fulfilled): asynchronous operation of Promise ended successfully
two。 Rejected (rejected): the asynchronous operation of Promise did not end successfully, which may be an error or caused by some other reason.
Once the state changes, it will be "solidified". It will remain in this state all the time and will not change again. When the state changes, the promise.then-bound function is called. Note: once a new Promise is created, it will be "executed immediately" and cannot be cancelled. This is also one of its shortcomings.
Thank you for your reading, the above is the content of "what is promise and how to use it". After the study of this article, I believe you have a deeper understanding of what promise is and how to use it, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.