飞镖太快了

时间:2014-03-23 13:58:17

标签: dart dart-async

我试图理解为什么这段代码打印“检查”两次......

import 'dart:io';
import 'dart:async';
import 'dart:convert';

Future<Map> ft_get_data() {
  File data;

  data = new File("data.json");
  return data.exists().then((value) {
    if (!value) {
      print("Data does no exist...\nCreating file...");
      data.createSync();
      print("Filling it...");
      data.openWrite().write('{"index":{"content":"Helllo"}}');
      print("Operation finish");
    }
  }).then((_) => data.readAsString()).then((content) => JSON.decode(content)
      ).catchError((e) => new Map());
}

void main() {
  Map params;
  String name;
  num check = 0;

  HttpServer.bind('127.0.0.1', 8080).then((server) {
    print("Server is lauching... $server");
    server.listen((HttpRequest request) {
      request.response.statusCode = HttpStatus.ACCEPTED;
      request.response.headers.contentType = new ContentType('text', 'htm    l');
      params = request.uri.queryParameters; // http://127.0.0.1:8080/?name=tristan
      ft_get_data().then((data_map) {
        name = data_map['index']['content'];
        print('check: $check');
        if (data_map.isNotEmpty) request.response.write(name); else
            request.response.write('Booh');
        check++;
      }).whenComplete(request.response.close);
    });
  }).catchError((error) {
    print("An error : $error.");
  });
}

太快了吗?还是有一些方法可以暂停?谢谢。

1 个答案:

答案 0 :(得分:1)

我尝试了您的代码并打印0

您使用什么客户端来访问服务器?