草庐IT

javascript - 带有异步等待的 chrome.runtime.onMessage 响应

我想在onMessage监听器中使用异步等待:chrome.runtime.onMessage.addListener(async(request,sender,sendResponse)=>{varkey=awaitgetKey();sendResponse(key);});但是,当我发送消息时,我得到了undefined。来自chrome.runtime.onMessage.addListener的文档:Thisfunctionbecomesinvalidwhentheeventlistenerreturns,unlessyoureturntruefromtheeventliste

javascript - 用于顺序执行同步和异步函数的 jQuery Deferred 和 Promise

如果我想让同步和异步函数以特定顺序执行,我可以使用jQuerypromise,但它似乎并没有像我期望的那样工作。当调用deferred.resolve()时,函数a、b和c应该按该顺序执行我希望函数b被执行,但所有函数都会立即执行决议被称为。代码如下:functiona(){vardeferred=$.Deferred();setTimeout(function(){console.log("statusina:",deferred.state());//thisshouldtriggercallingaornot?deferred.resolve("froma");},200);co

javascript - 如何解决单元测试中的异步等待 - javascript

我有一个lambda,我想为其编写单元测试。我正在使用asyncawait但我遇到了resolvepromises的问题。我想测试不同的条件,如何编写测试来解决和停止看到超时?提前致谢。Error:Timeoutof2000msexceeded.Forasynctestsandhooks,ensure"done()"iscalled;ifreturningaPromise,ensureitresolves.---单位describe('tests',function(){describe('describeanerror',()=>{it('shouldreturna500',(don

javascript - 链接异步方法调用 - javascript

您有一个带有两个异步方法调用的原型(prototype)对象Foo,bar和baz。varbob=newFoo()Foo.prototype.bar=functionland(callback){setTimeout(function(){callback()console.log('bar');},3000);};Foo.prototype.baz=functionland(callback){setTimeout(function(){callback()console.log('baz');},3000);};我们想要执行bob.bar().baz()并让它按顺序记录“bar”和

javascript - 在异步函数之外等待不会在控制台中抛出错误

MDNsays:Remember,theawaitkeywordisonlyvalidinsideasyncfunctions.Ifyouuseitoutsideofanasyncfunction'sbody,youwillgetaSyntaxError.但这不是真的。在DevTools控制台中尝试这段代码,没有错误,只有结果:asyncfunctiona(val){returnval;}awaita(10)//awaitisnotinsideasyncfunction10代码或文档有什么问题? 最佳答案 MDN文档是正确的,它解释

javascript - 通过blueimp jquery-fileupload异步上传多个文件

我正在使用jQuery文件上传库(http://github.com/blueimp/jQuery-File-Upload),我一直在弄清楚如何使用满足以下条件的库。该页面有多个文件输入字段,周围有一个表单标签。用户可以将多个文件附加到每个输入字段单击按钮时所有文件都会发送到服务器,而不是将文件附加到输入字段时。异步上传假设页面有3个输入字段,它们的名称属性分别为“file1[]”、“file2[]”和“file3[]”,请求有效负载应该类似于{file1:[arrayoffilesonfile1[]],file2:[file2[]上的文件数组],...这是jsFiddle,到目前为止

javascript异步/等待不工作

我有一个特殊情况,我需要等待异步调用结果继续。我正在使用async/await关键字,但运气不好。任何帮助表示赞赏。这是我尝试让它工作的尝试,数字应该按数字顺序排列。functionsleep(ms){returnnewPromise(resolve=>setTimeout(resolve,ms));}asyncfunctiondemo(){document.writeln('2...');awaitsleep(2000);document.writeln('3...');}document.writeln('1...');demo();document.writeln('4.');

javascript - 谷歌分析异步 : Events Tracking Callback?

我希望使用事件跟踪来记录对指向另一个网站的特定类型链接的点击。我正在使用jQuery,我目前拥有的代码是:$('a.website').click(function(event){varhref=$(this).attr('href');try{_gaq.push(['_trackEvent','website','click',href]);}catch(err){}});但是,在看到其他站点的referrer信息后,我不相信这是准确跟踪点击,可能是因为_gaq.push是异步的,并且在浏览器导航到url,并终止当前页面上运行的任何javascript。有什么方法可以检测到_gaq.

javascript - 使用带有异步函数和 .then 的 MobX @action 装饰器

我正在使用MobX2.2.2尝试在异步操作中改变状态。我将MobX的useStrict设置为true。@actionsomeAsyncFunction(args){fetch(`http://localhost:8080/some_url`,{method:'POST',body:{args}}).then(res=>res.json()).then(json=>this.someStateProperty=json).catch(error=>{thrownewError(error)});}我得到:Error:Error:[mobx]Invariantfailed:Itisnota

javascript - ES8 立即调用异步函数表达式

我没有看到这些构造被广泛使用,但我发现自己编写它们是为了在通常不会返回promise的函数中使用async/await,例如chan.consume(queue,(msg)=>{this.pendingMsgs++;//executedimmediately(async()=>{awaitthis.handleMessage(msg);this.pendingMsgs--;if(cancelled&&this.pendingMsgs===0){awaitchan.close();awaitthis.amqpConnectionPool.release(conn);}})();});相对