Javascript 链中有条件完成承诺

Javascript 链中有条件完成承诺,javascript,node.js,promise,ecmascript-6,bluebird,Javascript,Node.js,Promise,Ecmascript 6,Bluebird,我有一个承诺链,在这个承诺链中我执行了许多操作。当我达到某个then语句时,我想创建一个fork,它可能会继续这个链,但否则,将解决整个即将到来的承诺链 readFile('example.json').then(function (file) { const entries = EJSON.parse(file); return Promise.each(entries, function (entry) { return Entries.insertSync(en

我有一个承诺链,在这个承诺链中我执行了许多操作。当我达到某个then语句时,我想创建一个fork,它可能会继续这个链,但否则,将解决整个即将到来的承诺链

readFile('example.json').then(function (file) {
    const entries = EJSON.parse(file);
    return Promise.each(entries, function (entry) {
      return Entries.insertSync(entry);
    });
  }).then(function () {
    if (process.env.NODE_ENV === 'development') {
      return readFile('fakeUsers.json');
    } else {
      // I am done now. Finish this chain.
    }
  })
  // conditionally skip these.
  .then(() => /** ... */)
  .then(() => /** ... */)
  // finally and catch should still be able to fire
  .finally(console.log.bind('Done!'))
  .catch(console.log.bind('Error.'));

这可能与承诺有关吗?

您可以将条件then处理程序附加到条件本身中返回的承诺,如下所示

readFile('example.json').then(function (file) {
    return Promise.each(EJSON.parse(file), function (entry) {
      return Entries.insertSync(entry);
    });
  }).then(function () {
    if (process.env.NODE_ENV === 'development') {
      return readFile('fakeUsers.json')
        .then(() => /** ... */ )
        .then(() => /** ... */ );
    }
  })
  .finally(console.log.bind('Done!'))
  .catch(console.log.bind('Error.'));
  .finally(() => console.log('Done!'))
  .catch(() => console.log('Error.'));
如果您使用的是Node.js v4.0.0+,那么可以使用如下箭头函数

readFile('example.json').then(function (file) {
    return Promise.each(EJSON.parse(file), function (entry) {
      return Entries.insertSync(entry);
    });
  }).then(function () {
    if (process.env.NODE_ENV === 'development') {
      return readFile('fakeUsers.json')
        .then(() => /** ... */ )
        .then(() => /** ... */ );
    }
  })
  .finally(console.log.bind('Done!'))
  .catch(console.log.bind('Error.'));
  .finally(() => console.log('Done!'))
  .catch(() => console.log('Error.'));

您可以将条件then处理程序附加到条件本身中返回的承诺,如下所示

readFile('example.json').then(function (file) {
    return Promise.each(EJSON.parse(file), function (entry) {
      return Entries.insertSync(entry);
    });
  }).then(function () {
    if (process.env.NODE_ENV === 'development') {
      return readFile('fakeUsers.json')
        .then(() => /** ... */ )
        .then(() => /** ... */ );
    }
  })
  .finally(console.log.bind('Done!'))
  .catch(console.log.bind('Error.'));
  .finally(() => console.log('Done!'))
  .catch(() => console.log('Error.'));
如果您使用的是Node.js v4.0.0+,那么可以使用如下箭头函数

readFile('example.json').then(function (file) {
    return Promise.each(EJSON.parse(file), function (entry) {
      return Entries.insertSync(entry);
    });
  }).then(function () {
    if (process.env.NODE_ENV === 'development') {
      return readFile('fakeUsers.json')
        .then(() => /** ... */ )
        .then(() => /** ... */ );
    }
  })
  .finally(console.log.bind('Done!'))
  .catch(console.log.bind('Error.'));
  .finally(() => console.log('Done!'))
  .catch(() => console.log('Error.'));
你的代码是insertSync,应该是insertAsync吗?你的代码是insertSync,应该是insertAsync吗?