草庐IT

异步处理

全部标签

javascript - 如何在 Jquery Ajax 表单提交中添加错误处理

这是我的代码$(document).ready(function(){$('#spc-comment-flag-form').submit(function(){$.ajax({data:$(this).serialize(),type:$(this).attr('method'),url:$(this).attr('action'),success:function(data){if(data['error']==false){varmsg='Wegotyourflag.Ourmoderatorswillnowlookintoit.Youmayclosethewindownow!';

JavaScript 无法处理外部文件

当我在我的HTML文档中使用这段代码时,它起作用了:$('a.tocenter[href*=#]').click(function(){if(location.pathname.replace(/^\//,'')==this.pathname.replace(/^\//,'')&&location.hostname==this.hostname){var$target=$(this.hash);$target=$target.length&&$target||$('[name='+this.hash.slice(1)+']');if($target.length){vartargetO

javascript - 如何将 ng-repeat 分成批处理

我在http://getbootstrap.com/javascript/#buttons-examples做这个ref:checkbox例子{{type.name}}现在的问题是这个类型的数组有时少于5个元素,有时更多。当按钮组超过5个元素时,按钮组会以丑陋的方式拆分到下一行。我怎样才能做这样的事情ng-repeaton0-4ofarray-createabuttongroupforthese5itemsng-repeaton5-9ofarray(ifarraylengthis>5)...ng-repeaton10-14ofarray(ifarraylengthis>10).....

javascript - 处理 http 服务器崩溃

我有一个非常基本的http服务器:require("http").createServer(function(req,res){res.end("Helloworld!");}).listen(8080);如何监听服务器崩溃以便发送500状态代码作为响应?监听process.on("uncaughtException",handler)在process级别工作,但我没有请求和响应对象。我看到的一个可能的解决方案是在createServer回调中使用try-catch语句,但我正在寻找是否有更好的解决方案。我尝试在server对象上监听error事件,但没有任何反应:vars=requi

javascript - learnyounode #9 杂耍异步

我正在尝试学习nodeschool的learnyounode。Thisproblemisthesameasthepreviousproblem(HTTPCOLLECT)inthatyouneedtousehttp.get().However,thistimeyouwillbeprovidedwiththreeURLsasthefirstthreecommand-linearguments.YoumustcollectthecompletecontentprovidedtoyoubyeachoftheURLsandprintittotheconsole(stdout).Youdon'tn

javascript - 如何解决递归异步 promise ?

我在玩弄promises,我在处理异步递归promise时遇到了麻烦。场景是一位运动员开始跑100米,我需要定期检查他们是否跑完了,一旦他们跑完了,打印他们的时间。编辑以澄清:在现实世界中,运动员在服务器上运行。startRunning涉及对服务器进行ajax调用。checkIsFinished还涉及对服务器进行ajax调用。下面的代码试图模仿它。代码中的时间和距离是硬编码的,目的是让事情尽可能简单。抱歉没有说清楚。结束编辑我希望能够写出以下内容startRunning().then(checkIsFinished).then(printTime).catch(handleError)

javascript - 控制 javascript 异步流的速率(在循环中)

假设您想用一段短代码为列表中的每个文件夹启动一个(随机)进程:varexec=require('child_process').exec;varfolders=[...];//alistfromsomewhere_.each(folders,function(folder){exec("tarcvf"+folder+".tgz"+folder);});如果列表很长,我可能会同时运行大量进程,这是要避免的。以受控速率(此处最多5个并发进程)运行执行的相当简单的方法是什么?编辑:该问题适用于各种异步流(您希望在其中控制速率),而不仅仅是文件夹执行问题。 最佳答案

javascript - 羽毛笔链接处理程序不工作

我正在尝试为链接输入值编写自定义处理程序。如果用户输入的链接没有自定义协议(protocol),我希望在输入值之前添加一个http:。这是因为如果链接值缺少http:,则不会解释链接,而是显示about:blank。(https://github.com/quilljs/quill/issues/1268#issuecomment-272959998)下面是我写的(类似于官方的例子here):toolbar.addHandler("link",functionsanitizeLinkInput(linkValueInput){console.log(linkValueInput);//

javascript - 返回 Promise<void> 的异步函数是否在 block 末尾有隐式返回?

publicasyncdemo():Promise{//Dosomestuffhere//Doingmorestuff//...//Endofblockwithoutreturn;}是新的Promise在TypeScript/ES6的block末尾隐式返回?bool类型的例子:classTest{publicasynctest():Promise{returntrue;}publicmain():void{this.test().then((data:boolean)=>{console.log(data);});}}newTest().main();这会打印出true到控制台,因为r

javascript - Koa@2 错误处理

我正在使用Koa构建API。我使用koa-router设置了所有路由。每个路由都使用一个Controller,该Controller具有给定Mongoose模型的所有逻辑。我读过Koadocsonerror-handling并理解await在try/catchblock中的使用。他们在那里提到一个默认错误处理程序应该设置在中间件链的开头。因此,如果我有类似下面的内容,我应该在router.get()处对路由进行合理的错误处理:constKoa=require('koa');constRouter=require('koa-router');constapp=newKoa();const