fs.readFile(filePath, function(err, data) { if(err) {//处理错误,这里的return很重要,如果发生错误,在此处就会停止了。return console.log(err); } //传递data console.log(data);})你是如何避免回调地狱的?
new Promise((resolve, reject) => { setTimeout(() => {resolve("result"); }, 100)}) .then(console.log) .catch(console.error)stub是什么? 举个例子?
var fs = require("fs");var writeFileStub = sinon.stub(fs, "writeFile", function(path, data, cb) { return cb(null)})expect(writeFileStub).to.be.calledwriteFileStub.restore();如何保证你的HTTP cookies安全不受XSS攻击
Set-Cookit: sid=<cookit-value>; HttpOnly
new Promise((resolve, reject) => { throw new Error("error")}).then(console.log)then后面没有跟上catch,这样的话如果出错的这段代码还是默默地运行,并不会告诉你哪里出错了。
new Promise((resolve, reject) => { throw new Error("error")}).then(console.log).catch(console.error)如果你正在调试一个大型项目,你不知道哪个Promise可能会有问题,可以使用unhandledRejection。它会打印出所有未经处理的Promise异常
process.on("unhandledRejection", (err) => { console.log(err)})下面的代码有什么问题?
function checkApiKey(apiKeyFromDb, apiKeyReceived) { if (apiKeyFromDb === apiKeyReceived) {return true } return false}说实话我刚看到的时候也是一脸懵逼,这有啥问题?不是很正常的一个if else代码吗。
function checkApiKey(apiKeyFromDb, apiKeyReceived) { return cryptiles.fixedTimeCimparison(apiKeyFromDb, apiKeyReceived)}如何通俗地解释时序攻击(timing attack)?
下面的代码会输出什么
Promise.reso(1) .then((x) => x + 1) .then((x) => {throw new Error("My Error")}) .catch(() => 1) .then((x) => x + 1) .then((x) => console.log(x)) .catch(console.error)