Skip to content

Added _promisify to GJS GIO overrides

Avi requested to merge llzes/gjs:wip/overrides-gio-promisify into master

The _promisify function is a new internal feature for GJS, providing developers the option to write asynchronous operations with either the original callbacks from GNOME's original C (using GObject) libraries or to write them using async/await, a new modern way to write asynchronous code in JavaScript. This feature hides all of the original callbacks from developers in a Promise, allowing one to call the _async function like a variable by calling it with await in an async function. This is a backwards compatible feature, thus it will not impact already existing code using callbacks.

Please note that if you use this feature, to be ready for the official API version coming out in 3.32 where slight changes to your code will be necessary (in a good way, you can remove an extra line or two of your code)!

To use this feature, instead of nesting your _async and _finish function in your code (e.g. load_contents_async, load_contents_finish), you can update your program to an "async function", and call the _async function with an await leading it. You will also need to call the feature in your program, too, until the 3.32 release.

Example:

Gio._promisify(..., 'load_contents_async', 'load_contents_finish');
let [raw_content] = file.load_contents_async(cancellable);
Edited by Avi

Merge request reports