In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the new features of Node.js v15.x". The content of the explanation 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 are the new features of Node.js v15.x.
Simple exampl
Generally speaking, AbortController represents a controller object that allows us to abort one or more Web requests as needed.
The following is an example where the ac.abort () method is executed after 1 second, and the abort event is triggered, and only once, which can be seen through the abortSignal.aborted property to change the state before and after.
Ac.signal.addEventListener ('abort', () = > {console.log (' ac.signal.aborted:', ac.signal.aborted);}, {once: true}); setTimeout (() = > ac.abort (), 1000) console.log ('ac.signal.aborted:', ac.signal.aborted); abort the request
In Node.js, we can choose to use node-fetch as a request processing library to pass signal to fetch.
Assuming that the request needs to wait for 5 seconds, executing abort () in about 2 seconds will abort the request.
Const ac = new AbortController (); import fetch from 'node-fetch'; const timer = setTimeout (() = > ac.abort (), 2000) try {const {statusText} = await fetch (' http://localhost:3000/api', {signal: ac.signal}) console.log (statusText);} catch (err) {console.log (err.name); / / AbortError} finally {clearTimeout (timer);} abort Promise
Passing ac.signal aborts a running Promise, which requires us to register an abort event for ac.signal and do some processing. Then call ac.abort () anywhere to abort the Promise.
Any Web platform APIs that uses Promise to indicate aborting must follow the following principles:
Accept the AbortSignal object through an signal dictionary member.
The operation is aborted by reject a Promise with the class "AbortError" DOMException.
Check whether the aborted flag of the AbortSignal object has been set, and if so, reject immediately, otherwise:
Use the abort algorithm mechanism to observe changes to the AbortSignal object in a manner that does not lead to conflict with other observers.
The following implementation of doSomeThingAsync, an asynchronous Promise Function, basically follows these rules.
Class AbortError extends Error {constructor (message) {super (message); this.name = 'AbortError';}} function doSomethingAsync ({ac}) {return new Promise ((resolve, reject) = > {console.log (' task start...'); if (ac.aborted) {return reject (new AbortError ('task handler failed',' AbortError') } const timer = setTimeout (() = > {console.log ('task end...'); resolve (1);}, 5000); ac.signal.addEventListener (' abort', () = > {clearTimeout (timer); reject (new AbortError ('task handler failed',' AbortError'));}, {once: true});}) } setTimeout (() = > ac.abort (), 2000) try {await doSomethingAsync ({ac});} catch (err) {console.error (err.name, err.message); / / AbortError task handler failed}
Note: there is currently no DOMException class in Node.js, and we can't do this with new DOMException ('task handler failed',' AbortError'), so I started by creating an AbortError class to simulate.
There are already some asynchronous API in Node.js that support passing signal, but its DOMException errors are also internally encapsulated:
/ / https://github.com/nodejs/node/blob/f6b1df2226/lib/internal/fs/promises.js#L98 const lazyDOMException = hideStackFrames ((message, name) = > {if (DOMException = undefined) DOMException = internalBinding ('messaging'). DOMException; return new DOMException (message, name);}) / / for example, writeFileHandle / / https://github.com/nodejs/node/blob/f6b1df2226/lib/internal/fs/promises.js#L282 if (signal?.aborted) {throw lazyDOMException ('The operation was aborted',' AbortError') } Thank you for your reading, the above is the content of "what are the new features of Node.js v15.x". After the study of this article, I believe you have a deeper understanding of what the new features of Node.js v15.x have, 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.