Prototype 2 Finally Full

0 views
Skip to first unread message

Oliverio Gallman

unread,
Aug 5, 2024, 9:34:44 AM8/5/24
to gaugrineltrom
Thefinally() method of Promise instances schedules a function to be called when the promise is settled (either fulfilled or rejected). It immediately returns another Promise object, allowing you to chain calls to other promise methods.

Returns a new Promise immediately. This new promise is always pending when returned, regardless of the current promise's status. If onFinally throws an error or returns a rejected promise, the new promise will reject with that value. Otherwise, the new promise will settle with the same state as the current promise.


ES Proposal spec-compliant shim for Promise.prototype.finally. Invoke its "shim" method to shim Promise.prototype.finally if it is unavailable or noncompliant. Note: a global Promise must already exist: the es6-shim is recommended.


The most common use case is similar to the most common use case of the synchronous finally clause: cleaning up after you are done with a resource. That should always happen, regardless of whether everything went smoothly or there was an error.


ES2018 introduced a new Promise.prototype.finally() method to the standard library. The finally() method lets you execute a callback function once the promise that it's called on has been settled (that is, it has been either fulfilled or rejected and is therefore no longer pending):


If you see this error, the compiler is telling you that it doesn't know anything about a finally() method on the Promise type. In that case, the JavaScript version you're targeting (typically ES5 or ES2015) doesn't define a finally() method on the Promise prototype.


The solution to make the type error go away is to assure the TypeScript compiler that at run-time, the finally() method is going to be available (either natively or via a polyfill). Head over to your project's tsconfig.json file and add the value "es2018.promise" to the "lib" array:


If you need to support a browser that doesn't implement the finally() method natively, make sure to include a polyfill in your application (e.g. via the promise.prototype.finally npm package). Note that if Promise itself isn't supported in any of your target browsers, you'll need to polyfill that separately.


Since the Promises were added in JavaScript, one of the biggest concerns was the absence of an ability to easily applysome code after the Promise is fulfilled (after both success and reject actions).There are many examples of such a need:showing a loader when the request to the server is in flightor even a simpler case- when we just want to log the completion of the operation regardless of how it finished.


Availability of Promise.prototype.finally() brings it to all the Promise-based API, like the global fetch() (used in the demos)and all the Promise-based code you have in your projects.Which means, as only .finally() is supported or polyfilled, you can use it across your codebase for Promises.


Promise.prototype.finally() recently reached stage 4 of the TC39 proposal process. This means the Promise.prototype.finally() proposal was accepted and is now part of the latest draft of the ECMAScript spec, and it is only a matter of time before it lands in Node.js. This article will show you how to use Promise.prototype.finally() and how to write your own simplified polyfill.


Note that .then() takes two function parameters. The first is onFulfilled(), which gets called if the promise is fulfilled. The second is onRejected(), which gets called if the promise rejects. A promise is a state machine that can be in one of 3 states:


Like .catch(), the .finally() function is a convenient shorthand for .then(). The difference is that .finally() executes a function onFinally when the promise is settled, that is, when it is either fulfilled or rejected. The .finally() function is not part of any Node.js release at the time of this writing, but the promise.prototype.finally module on npm has a polyfill.


The above script will print both 'fulfilled' and 'rejected', because the onFinally handler gets called when the promise is settled, regardless of whether it is fulfilled or rejected. However, the onFinally handler does not receive any parameters, so you can't tell whether the promise was fulfilled or rejected.


The finally() function returns a promise, so you can chain more .then(), .catch(), and .finally() calls onto the return value. The promise that finally() returns will fulfill to whatever the promise it was chained onto would fulfill to. For example, the below script will still print 'foo', even though the onFinally handler returns 'bar'.


The above script demonstrates an important detail of working with finally(): finally() does not handle promise rejections for you. How finally() handles promise rejections merits more careful study.


The finally() function is not meant to handle promise rejections. As a matter of fact, it explicitly rethrows errors after your onFinally() function executes. The below script will print an unhandled promise rejection warning.


However, the finally() function returns a promise, so there's nothing stopping you from chaining .catch() after .finally(). In particular, if your onFinally handler can error out, for example if it makes an HTTP request, you should add a .catch() at the end to handle any errors that may occur.


I think that the easiest way to really grok something is to write your own implementation. The .finally() function is a good candidate for this because the official polyfill is only 45 lines, and most of it isn't necessary for a simple proof of concept.


The key idea behind this implementation is that the onFinally handler may return a promise. If it does, you need to .then() on that promise and resolve or reject with what the initial promise settled to. You can explicitly check if the return value from the onFinally handler is a promise, but Promise.resolve() already does that for you and saves you several if statements. You also need to make sure you track the value or error the initial promise settled to, and make sure the returned promise from finally() either fulfills to the initial resolved value res, or rethrow the initial rejected error err.


The Promise.prototype.finally() function is one of 8 stage 4 TC39 proposals at the time of this writing, which means finally() and 7 other new core language features are coming to Node.js. The finally() function is one of the most exciting of the 8 new features, because it promises to make cleaning up after async operations much cleaner. For example, below is code that I have running in production right now that desperately needs finally() for releasing a lock after a function is done executing.


Confused by promise chains? Async/await is the best way to compose promises in Node.js.Await handles promise rejections for you, so unhandled promise rejections go away. My new ebook, Mastering Async/Await, is designed to give you an integrated understanding ofasync/await fundamentals and how async/await fits in the JavaScript ecosystem in a few hours. Get your copy!


Looking forward, SpaceX is expected to fly several more prototypes in Texas this year. Ultimately, the company hopes to launch on full-fledged missions from Texas, Florida, and possibly even ocean-based platforms.


The Starship program has been in the headlines recently for non-test-related events, too. NASA recently announced it had awarded SpaceX $2.9 billion specifically for Starship, which is envisioned as the lander that will take the next set of American astronauts from lunar orbit down to the surface.


Work on the contract, known as Human Landing System or HLS, is currently on hold due to a protest filed by Blue Origin. The Jeff Bezos-led company was also in the running for an HLS contract, but NASA opted not to go with its proposal due to a variety of reasons ranging from price to the submission process.


Regardless of the HLS contract, Musk sees Starship as the vehicle that will help SpaceX fulfill its vision of putting human boots on Mars. He ultimately wants hundreds of people traveling to the red planet in each Starship.


HPE unveiled the prototype at an event in London on Monday. The Machine wasn't actually powered up in London; rather, it's located in a lab in Fort Collins, Colorado. There was a single node of the prototype on display in London, though. The chassis is very deep (about a foot deeper than a usual server rack) and thin, with an SoC on one end, RAM in the middle, and then oodles (2-4 terabytes) of persistent memory taking up the rest of the case.


Persistent memory isn't the only exciting bit, though: the SoC is attached to the persistent memory via a silicon photonics fabric. Presumably those ribbons that connect to the SoC and run along the edge of the chassis are fibre-optic cabling. HPE says that the SoC can also use the persistent memory in other nodes, again via a silicon photonics fabric, resulting in hundreds or thousands of terabytes of total accessible memory.


The main purpose of the event in London, though, wasn't to talk about the prototype; rather, it was to quietly kill off hopes that The Machine as an actual, er, machine, will be commercialised. One of the slides (pictured above) explicitly stated the Machine's goal is now to "demonstrate progress, not develop products." The next slide then shows how some of the tech advances will percolate down into other HPE products.


Still, a lot has changed since The Machine was first conceived, including HP splitting into two separate companies. The company also expected memristor-based non-volatile memory to be commercialised by now. Pivoting The Machine to a technology incubator seems like a very sensible idea, but completely reinventing the architecture of computing in a series of baby steps is of course a little less exciting than the original moonshot.

3a8082e126
Reply all
Reply to author
Forward
0 new messages