Commit 5919962a authored by Avi's avatar Avi

load_contents_promise now working with cancellable!

parent 9be85871
const {GLib, Gio} = imports.gi;
const cancel = new Gio.Cancellable();
let loop = GLib.MainLoop.new(null, false);
const load_contents_promise = (file)=>{
const load_contents_promise = (cancellable = null, file)=>{
return new Promise((resolve, reject)=>{
file.load_contents_async(null, function(f, res) {
file.load_contents_async(cancellable, function(f, res) {
try {
let result = f.load_contents_finish(res);
resolve(result);
......@@ -17,14 +18,13 @@ const load_contents_promise = (file)=>{
const head = async(filename)=>{
let file = Gio.file_new_for_commandline_arg(filename);
let raw_content;
let content_formatted;
try {
raw_content = await load_contents_promise(file);
let [, raw_content] = await load_contents_promise(null, file);
/* Prints 'true' as load was successful */
// content_formatted = raw_content[0].toString();
/* Get only the results with raw_content[1] instead of printing all of the values */
content_formatted = raw_content[1].toString();
content_formatted = raw_content.toString();
/* Prints '1527301825:698952' for the location of the contents */
// content_formatted = raw_content[2].toString();
print(content_formatted.split('\n', 10).join('\n'));
......@@ -39,6 +39,7 @@ if (ARGV.length !== 1) {
printerr('Usage: gio-head.js filename');
} else {
head(ARGV[0]);
cancel.cancel();
loop.run();
}
......
......@@ -14,9 +14,9 @@
error GError a GError, or `NULL`
);
*/
const load_contents_promise = (file)=>{
const load_contents_promise = (cancellable = null, file)=>{
return new Promise((resolve, reject)=>{
file.load_contents_async(null, function(f, res) {
file.load_contents_async(cancellable, function(f, res) {
try {
let result = f.load_contents_finish(res);
resolve(result);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment