草庐IT

promised-mongo

全部标签

Javascript:如何使用 promise 迭代数组?

LIVEDEMO给定以下函数:functionisGood(number){vardefer=$q.defer();$timeout(function(){if(){defer.resolve();}else{defer.reject();}},100);returndefer.promise;}和一组数字(例如[3,9,17,26,89]),我想找到第一个“好”的数字。我希望能够做到这一点:vararr=[3,9,17,26,89];findGoodNumber(arr).then(function(goodNumber){console.log('Goodnumberfound:'

javascript - 在 JavaScript 中扁平化 promise

bluebird库似乎自动使用Promise::then作为promise上的“map”和“flatMap”的等价物,例如参见这个例子。varPromise;Promise=require('bluebird').Promise;Promise.resolve(1).then(function(x){returnPromise.resolve(x+1);}).then(function(x){returnconsole.log(x);//=>`2`(notapromise)});Promise.resolve(1).then(function(x){returnx+1;}).then(

javascript - 使用 promise 等待触发的事件

在下面的代码中,thing是一个我无法控制的外部对象;我无法更改thing的事件系统的工作方式。当fn被调用时,我们返回一个promise,其执行者监听一个事件,然后开始等待一系列最终导致该事件被触发的函数:functionfn(){returnnewPromise(asyncfunction(resolve,reject){//Thishandlermustbeattachedbefore`c`iscalledthing.once('myEvent',function(e){resolve(e.data);//done});//Theorderofthesefunctionscall

javascript - 如何在 when 函数中延迟 promise

如何延迟promise链?我需要这个,因为我想在继续执行脚本之前等待CSS动画完成。该函数的目的是打开一个View。如果View尚未打开,则打开它(通过更改类),等待css动画,继续。如果View已经打开,则什么都不做并继续。我想这样调用函数:(它是AngularController中的一个函数)$scope.openView(viewId).then(function(){$scope.openAnotherView(anotherViewId);});/**Functiontoopensomeview**/$scope.openView=function(viewId){funct

javascript - 返回一个等待的值会返回一个 Promise? (es7 异步/等待)

constret=()=>newPromise(resolve=>setTimeout(()=>resolve('somestring'),1000));asyncfunctionwrapper(){letsomeString=awaitret();returnsomeString;}console.log(wrapper());它记录Promise{};为什么它返回一个Promise而不是'somestring'?我正在使用BabelES7预设来编译它。 最佳答案 异步函数返回promise。为了做你想做的事,试试这样的事情wra

javascript - 使用 q.js 链接 promise

我正在尝试了解promise链的工作原理。我正在使用q.js.这就是我正在玩的东西。varQ=require("q");//npminstallq//thefunctionQ(value)returnsafulfilledpromisewiththevalue...Ithink.Q(1).then(Q(2)).then(Q(3)).then(Q(4)).then(function(n){console.log(n);});Q(1).then(function(n){returnQ(2);}).then(function(n){returnQ(3);}).then(function(n)

javascript - AngularJs 单元测试 - 模拟 promise 不执行 "then"

我们正在对我们的Controller进行单元测试。我们已经成功地模拟了对REST服务层的调用,并验证了它确实被给定的数据调用了。然而,现在我们想在我们的Controller中测试thenpromise的执行是否改变了location.path:Controller:(function(){app.controller('registerController',['$scope','$location','$ourRestWrapper',function($scope,$location,$ourRestWrapper){$scope.submitReg=function(){//te

javascript - 如何在 PapaParse 中使用 Promises?

PapaParse的API有一个异步回调函数。我想知道如何将其转换为promise。例如:Papa.parse(fileInput.files[0],{complete:function(results){console.log(results);}});如有任何帮助,我们将不胜感激! 最佳答案 基本模式是Papa.parsePromise=function(file){returnnewPromise(function(complete,error){Papa.parse(file,{complete,error});});};然

javascript - JavaScript Promise.all 是否有一个在成功和失败时触发的回调

这个问题在这里已经有了答案:Waituntilallpromisescompleteevenifsomerejected(20个答案)关闭6年前。我是不是误解了Promise.all?我在数组中有X个promise,我正在尝试汇总数组的成功/失败比率。这是我认为我知道的:Promise.all采用一系列promise。如果所有的promise都成功,那么.then回调就会运行。如果其中一个promise失败,则调用.catch回调,传入的参数是单个引发错误的值。没有触发回调,这是所有promise的结果,如果有些成功,有些失败。IE。它不能给你一个像(伪代码)[success,fail

javascript - Promise.all 返回空对象

我正在尝试使用Promise.all一次从电影数据库中获取多个数据对象。在我遍历fetch调用的所有结果并对每一位数据使用.json()之后,我尝试将其记录到控制台。但是,我得到的不是一组包含数据的对象,而是一组Promises。嵌套在promises中,我可以看到我的数据,但我显然缺少一个步骤来拥有一组数据对象,而不仅仅是Promises。我在这里错过了什么?//storemovieAPIURLsintomeaningfulvariablesconsttrending=`https://api.themoviedb.org/3/trending/all/day?api_key=${A