如何在Javascript

时间:2015-07-17 07:59:45

标签: javascript node.js callback promise

我已经开发了一个命令行程序及其工作,但是当它完成时它没有完成。我必须控制+ c一个shell退出

我是javascript的新手。我必须将一个回调称为该程序完成它吗?

我的代码:

importer.then(function (csvData) {
    var mySqlConnection = new MySqlConnection(config['phpipam-db']['host'], config['phpipam-db']['user'], config['phpipam-db']['password'], config['phpipam-db']['database']);
    var connection = mySqlConnection.getConnection();

    mySqlConnection.selectSubnets(connection).then(function (customerFolders) {
        var ipv6Data = manager.getIpv6CustomerSubnetsInsertData(csvData, customerFolders);
        var ipv4Data = manager.getIpv4CustomerSubnetsInsertData(csvData, customerFolders);

        mySqlConnection.insertIpv6Subnets(ipv6Data);
    });
});

2 个答案:

答案 0 :(得分:0)

console.logvar connection = mySqlConnection.getConnection();之后添加mySqlConnection.insertIpv6Subnets(ipv6Data);个语句。

如果它们被执行,则问题不在您发布的代码中。

如果不是,你必须检查你的承诺是否得到满足。

答案 1 :(得分:0)

这种方法:manager.getIpv6CustomerSubnetsInsertDatamySqlConnection.insertIpv6Subnets是否也会返回承诺?

如果你修改这样的代码,你能在控制台中看到完成吗?

importer.then(function (csvData) {
  var mySqlConnection = new MySqlConnection(config['phpipam-db']['host'], config['phpipam-db']['user'], config['phpipam-db']['password'], config['phpipam-db']['database']);
  var connection = mySqlConnection.getConnection();

  return mySqlConnection.selectSubnets(connection).then(function (customerFolders) {
    var ipv6Data = manager.getIpv6CustomerSubnetsInsertData(csvData, customerFolders);
    var ipv4Data = manager.getIpv4CustomerSubnetsInsertData(csvData, customerFolders);

    return mySqlConnection.insertIpv6Subnets(ipv6Data);
  }).then(function () {
    console.log('Done');
  });
});