readAsyncWithCallback

Read file path with asynchronous IO then calls the callback function. If file size is larger than up_to, only up_to bytes read.

If an error occurred, the callback function will be called with the error.

  1. void readAsyncWithCallback(string path, size_t up_to, void delegate(void[], Exception) nothrow callback)
  2. void readAsyncWithCallback(string path, void delegate(void[], Exception) nothrow callback)
    nothrow @safe
    void
    readAsyncWithCallback
    (
    string path
    ,
    void delegate
    (
    void[]
    ,
    Exception
    )
    nothrow
    callback
    )

Parameters

path string

string repreesenting file path.

callback void delegate
(
void[]
,
Exception
)
nothrow

a fuction called when operation finished or an error occurred.

Examples

import std.file : write, remove;
import dpromise.utils : runEventloop;
write("hoge.txt", "hogehogepiyopiyo");
scope(exit) remove("hoge.txt");

readAsyncWithCallback("hoge.txt", size_t.max, (data, e) nothrow {
  try {
    assert(e is null);
    assert(data !is null);

    assert(cast(string)data == "hogehogepiyopiyo");
  } catch(Exception e) {}
});

readAsyncWithCallback("hoge.txt", 8, (data, e) nothrow {
  try {
    assert(e is null);
    assert(data !is null);

    assert(cast(string)data == "hogehoge");
  } catch(Exception e) {}
});

runEventloop();
import std.file : write, remove;
import dpromise.utils : runEventloop;

write("hoge.txt", "hogehogepiyopiyo");
scope(exit) remove("hoge.txt");

readAsyncWithCallback("hoge.txt", (data, e) nothrow {
  try {
    assert(e is null);
    assert(data !is null);

    assert(cast(string)data == "hogehogepiyopiyo");
  } catch(Exception e) {}
});

runEventloop();

Meta