Tag: es6 promise

Promise.resolve()。然后vs setImmediate vs nextTick

NodeJS 0.11以及io.js和Node 0.12分支均附带原生承诺。 Native承诺有一个.then方法 ,总是在将来的事件循环中执行。 到目前为止,自从我从nextTick切换到现在,我一直使用setImmediate将事物setImmediate到事件循环的下一个迭代中: setImmediate(deferThisToNextTick); // My NodeJS 0.10 code process.nextTick(deferThisToNextTick); // My NodeJS 0.8 code 由于我们现在有一个新的方法来做到这一点: Promise.resolve().then(deferThisToNextTick); 我应该用哪个? 另外 – Promise.resolve.then行为像setImmediate或像nextTick关于事件循环之前或之后运行的代码?

Express应用程序中未处理的拒绝

我有很多基于ES6承诺的代码在我的快车应用程序中运行。 如果有一个从未被捕获的错误,我使用下面的代码来处理它: process.on('unhandledRejection', function(reason, p) { console.log("Unhandled Rejection:", reason.stack); process.exit(1); }); 这适用于debugging目的。 但在生产中,我想触发500error handling程序,向用户显示标准的“出错”页面。 我有这个捕获所有error handling程序,目前适用于其他例外: app.use(function(error, req, res, next) { res.status(500); res.render('500'); }); 将未处理的拒绝放在中间件内不起作用,因为它是asynchronous的,并导致Error: Can't render headers after they are sent to the client. 我将如何去unhandledRejection一个unhandledRejection拒绝500页?