在每个Promise.then函数

时间:2016-05-30 11:49:32

标签: javascript node.js promise bluebird

我正在开发一个nodejs应用程序并使用bluebird.js - Promise。我有一个方法,我想在链中执行每个.then()方法后调用。是否有一种方法(或API)支持bluebird.js?

感谢。

2 个答案:

答案 0 :(得分:0)

您可以使用bluebird监控API。

let currentViewController:UIViewController=UIApplication.sharedApplication().keyWindow!.rootViewController!

interstitial.presentFromRootViewController(currentViewController)

收听Promise.config({ monitoring: true }); 事件以检测promiseChained是否被链接,然后收听then事件以了解回调何时执行:

promiseFulfilled

这将在执行的每个 var candidates = new WeakSet(); // a set for our promises that were chained process.on('promiseChained', (_, {promise, child}) => { candidates.add(child); }); process.on('promiseFullfilled', (_, {promise}) => { if(candidates.has(promise)) { callYourFunctionHere(); // HERE } }); 处理程序上执行,如果你想在特定的中执行它,那么在添加到候选人之前 - 检查是否{{ 1}}(参数)是链接到它的集合或它在集合中的承诺(所以它是链的一部分)。

答案 1 :(得分:0)

我为每个人创建一个特殊的回调。例如:

假设您在"然后" s上有3种不同的回调。

function callbackA() {
  console.log('A');
  return Promise.resolve();
}
function callbackB(hello) {
  console.log('B',hello);
  return Promise.resolve();
}
function callbackC() {
  console.log('C');
  return Promise.resolve();
}

//Then you have your "then" handler:

function handler(cb, context, params) {
    context = context || this;
  params = params || [];
  //Do your "on-every-then stuff
  console.log("It's a THEN!!!");
  //Then run your desired callback
  return cb.apply(context, params);
}

//And your main code:
callbackA()
  .then(handler(callbackB, this, ["there"]))
  .then(handler(callbackC));

小提琴here