如何链接多个承诺?

时间:2015-05-22 19:55:02

标签: javascript node.js promise bluebird fs

我不太确定,也许我错过了一些明显的东西,但我无法弄清楚如何将两个承诺联系在一起。

我的基于回调的代码如下所示:

async.series([
function (cb) {
  // Create the directory if the nodir switch isn't on
  if (!nodir) {
    fs.mkdir('somedirectory', function (err) {
      if (err) {
        log('error while trying to create the directory:\n\t %s', err)
        process.exit(0)
      }
      log('successfully created directory at %s/somedirectory', process.cwd())
      cb(null)
    })
  }
  cb(null)
},
function (cb) {
  // Get the contents of the sample YML
  fs.readFile(path.dirname(__dirname) + '/util/sample_config.yml', function (err, c) {
    var sampleContent = c
    if (err) {
      log('error while reading the sample file:\n\t %s', err)
      process.exit(0)
    }
    log('pulled sample content...')
    // Write the config file
    fs.writeFile('db/config.yml', sampleContent, function (err) {
      if (err) {
        log('error writing config file:\n\t %s', err)
        process.exit(0)
      }
      log('successfully wrote file to %s/db/config.yml', process.cwd())
      cb(null)
    })
  })
}
])

我已经开始尝试将此重构为基于承诺的流程,这是我到目前为止所做的:

if (!nodir) {
fs.mkdirAsync('somedirectory').then(function () {
  log('successfully created directory at %s/somedirectory', process.cwd())
}).then(function () {
  // how can I put fs.readFileAsync here? where does it fit?
}).catch(function (err) {
  log('error while trying to create the directory:\n\t %s', err)
  process.exit(0)
})
}

(我正在使用Bluebird,所以我之前做过Promise.promisifyAll(fs)

问题是,我不知道在哪里放置前一系列的第二个“步骤”。我把它放在then或其功能中吗?我是否将它归还并将其置于单独的功能中?

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

通常,承诺驱动的代码如下所示:

operation.then(function(result) {
  return nextOperation();
}).then(function(nextResult) {
  return finalOperation();
}).then(function(finalResult) {
})

你的例子中有很多事情发生,但总的想法是:

Promise.resolve().then(function() {
  if (nodir) {
    return fs.mkdir('somedirectory').catch(function(err) {
      log('error while trying to create the directory:\n\t %s', err);
      process.exit(0);
    });
  }
}).then(function() {
  return fs.readFile(path.dirname(__dirname) + '/util/sample_config.yml').catch(function(err) {
    log('error while reading the sample file:\n\t %s', err);
    process.exit(0);
  })
}).then(function(sampleContent) {
  log('pulled sample content...');

  return fs.writeFile('db/config.yml', sampleContent).catch(function(err) {
    log('error writing config file:\n\t %s', err)
    process.exit(0)
  })
}).then(function() {
  log('successfully wrote file to %s/db/config.yml', process.cwd())
})

这假设你正在使用的所有电话都是本机的。

由于您的第一步是有条件的,因此原始的Promise.resolve()只是从承诺开始的链条。

答案 1 :(得分:2)

由于你正在使用Bluebird,你可以使用大量的糖,并且代码比公认的答案IMO清晰得多:

-[Gift setGift:]: unrecognized selector sent to instance 0x7ffb7b45dd20

我会更进一步,并且会删除您在此处遇到的更细粒度的错误,因为您使用的类型实际上没有提供这些API方法提供的内置消息的其他信息 - 将代码缩短为:

var fs = Promise.promisifyAll(fs); // tell bluebird to work with FS
Promise.try(function(){ 
  if(nodir) return fs.mkdirAsync('somedirectory').
                      catch(catchErr("Could not create dir"));
}).then(function(){
    return fs.readFileAsync(path.dirname(__dirname) + '/util/sample_config.yml').
             catch(catchErr("error while reading the sample file"));
}).then(function(data){
  log('pulled sample content...');
  return fs.writeFile('db/config.yml', data).
            catch(catchErr("error writing config file"));
}).then(function(){
  log('successfully wrote file to %s/db/config.yml', process.cwd())
}, function(err){
    // centralized error handling, to remove the redundancy
    log(err.message);
    log(err.internal);
    log(err.stack); // this is important! 
});


function catchErr(msg){ // helper to rethrow with a specific message
    return function(e){
        var err = new Error(msg);
        err.internal = e; // wrap the error;
        throw e;
    };
}

Promise是安全的,并提供理智和证明的错误处理 - 如果你问我,相当不错的胜利。它会变得更好,如果您使用io.js或现代节点,您可以使用:

Promise.try(function(){
    if(nodir) return fs.mkdirAsync("somedirectory");
}).then(function(){
    fs.readFileAync(path.dirname(__dirname) + '/util/sample_config.yml');
}).then(function(data){
    log('pulled sample content...');
    return fs.writeFile('db/config.yml', data);
}).then(function(){
  log('successfully wrote file to %s/db/config.yml', process.cwd())
}).catch(function(err){
    log(err);
    process.exit(1);
});