草庐IT

手写Promise

全部标签

javascript - 在 promise 中使用 then()

我在下面的代码块中使用了promise,2firstthen()我没有返回任何值,所以我想知道第一个then()中的函数是在下一个then()中的函数之前调用的。我在Angular2中使用typescript。谢谢:DPromise.resolve().then(()=>{this.getListStatus();}).then(()=>{returnthis._laundryServiceOrderService.findAll(true,offset,this.itemsPerPage,filterQuery)}).then((response)=>{this.orders=res

JavaScript ES6 - promise 解决和事件之间可能存在竞争条件?

是否可以在promise解析和promise等待返回之间执行用户代码?functiona(){returnnewPromise(resolve=>{setTimeout(()=>{//Betweenhere...resolve()},1000))}}asyncfunctionb(){awaita()//...andhere?}规范是否强制立即调用Promise回调?我想知道2点之间的虚拟机是否可以处理一个事件,这可能会导致副作用。 最佳答案 不,它不强制立即调用。Thespec在解决一个promise时会经历许多步骤,其中之一是:P

javascript - 为什么 Promise 没有 get() 函数?

如果您知道Promise已经被解决,为什么不能直接调用get()并接收值呢?与将then(..)与回调函数一起使用相反。所以不要这样做:promise.then(function(value){//dosomethingwithvalue});我希望能够做的更简单:varvalue=promise.get();Java为它的CompletableFuture提供了这个我看不出为什么JavaScript不能提供相同的功能。 最佳答案 Java的get方法“如有必要,等待此future完成”,即它会阻塞当前线程。我们绝对不想在只有一个“

javascript - Promise 不会等待 for 循环完成

我有一个异步代码,我想在我的一个Nodejs脚本中同步运行,但这不会等待代码块完成并解析空对象-newPromise((resolve,reject)=>{if(object.email!==undefined){for(leti=0;i{console.log('>>isEmailUnsubscribedresultinsendemailnotification:'+result)if(!result){emailObjects.push(emailObject.EmailID)}})}}console.log('emailObjects')console.log(emailObje

javascript - 有没有一种好的方法可以缩短 Javascript 的 promise ?

我对promises/Deferreds有点陌生。对于成功和错误的情况,是否有一个好的模式来处理可能想要缩短promise链的情况?在错误情况下,我知道你可以将一个.then(null,function(error){})链接到最后并从之前的任何一个then中捕获错误,但是如果你想以更自定义的方式处理错误并终止?您是否会在较早的错误处理程序中指定错误的“类型”并通过新的promise返回它,以在最终的错误处理程序中处理或跳过?如果您想在链中更早地终止(仅有条件地触发任何稍后的then)的成功案例呢? 最佳答案 通常,promise链

javascript - 解决后未将 promise 值放入模板

我对javascript和promises还很陌生,所以我可能无法理解所有的基本概念,但我正在尝试。我的模型中有一个检查友谊状态的函数:friendShipStatus:function(){varself=this;returnEmber.RSVP.all([this.container.lookup('user:current'),this.get('myFriends'),this.get('friendsWithMe'),this.get('friends')]).then(function(result){varuser=result[0],myFriends=result[

javascript - jQuery promise 是否符合 Promises/A+

我已阅读thisarticle这是基于著名的DomenicDenicola'sarticle.第一个说:TheproblemwithjQuery’simplementation(upuntilversion1.9)isthatitdoesn’trespectthesecondpartofthespecification,“Thisfunctionshouldreturnanewpromise…”,thatis“then”doesn’treturnanewpromiseobjectwhenexecutingoneofthehandlers(eitherthefullfillment,th

javascript - 如何根据以下内容决定哪个 promise 执行 then/catch

全部:我是Promise的新手,这里有一个例子:varsomeAsyncThing=function(){returnnewPromise(function(resolve,reject){//thiswillthrow,xdoesnotexistresolve(x+2);});};varsomeOtherAsyncThing=function(){returnnewPromise(function(resolve,reject){reject('somethingwentwrong');});};someAsyncThing().then(function(){returnsomeO

javascript - 将 promise 包装在 promise 中是一种反模式吗?

我尝试使用PromiseAPI编写代码以超时重新连接到数据库。我最终所做的是将连接到数据库的promise包装在一个promise中,但我不确定这是否是最好的做事方式。我认为可能有一种方法可以使用尝试连接到数据库的原始promise,但我无法弄清楚。functionconnect(resolve){console.log('Connectingtodb...');MongoClient.connect(url,{promiseLibrary:Promise}).then((db)=>resolve(db)).catch((err)=>{console.log('dbconnection

javascript - AngularJS 中的 promise 顺序

问题:是否有一种“简单”的方法来取消AngularJS中的($q-/$http-)promise或确定解决promise的顺序?示例我有一个长时间运行的计算,我通过$http请求结果。在初始promise得到解决之前,某些操作或事件要求我重新启动计算(并因此发送新的$http请求)。因此我想我不能使用像这样的简单实现$http.post().then(function(){//applydatatoview})因为我无法确保响应按照我发送请求的顺序返回-毕竟我想在所有promise都得到正确解决时显示最新计算的结果。但是我想避免等待第一个响应,直到我发送这样的新请求:consttime