我正在寻找一种使用 Promise 的方法,例如 JavaScript 或 ES5 中的 Promise。
我想做这样的事情:
auto p = new Promise!string();
webRequest.get("server.com/file.json").then((v) {
auto json = ParseData(v);
auto fileContent = fileIO.readFile(json.filename).then((v2) {
p.resolve(v2);
});
});
D有办法做这样的事情吗?
最佳答案
http://vibed.org/api/vibe.core.concurrency/Future
import vibe.core.concurrency : async;
import vibe.inet.urltransfer : download;
import vibe.data.json : parseJsonString;
import vibe.core.file : readFileUTF8;
auto p = async({
auto content = download("server.com", "file.json");
auto json = parseJsonString(content);
auto file_content = readFileUTF8(json["filename"]);
});
auto content = p.getResult();
但是这里实际上不需要使用async
,因为vibe.d的所有I/O函数已经是100%异步的,不需要任何回调。
关于promise - D 中有类似 Promise 或 Tasks 的东西吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37089675/