草庐IT

promises

全部标签

javascript - 当有很多 promise 时解析 : Promise.?

Parse文档(https://www.parse.com/docs/js/symbols/Parse.Promise.html#.when)解释说,在使用Parse.Promise.when时,指定一个promise数组是合乎规范的:varp1=Parse.Promise.as(1);varp2=Parse.Promise.as(2);varp3=Parse.Promise.as(3);varpromises=[p1,p2,p3];Parse.Promise.when(promises).then(function(r1,r2,r3){console.log(r1);//prints

javascript - 当一个 promise 依赖于另一个 promise 时,Bluebird 的 Promise.all() 方法

我正在编写一些目前看起来像这样的代码,因为我的代码中有依赖项。我想知道使用Promise.all()是否有更简洁的方法来做到这一点?这是我的伪代码:returnsomeService.getUsername().then(function(username){user=username;}).then(function(){returnsomeService.getUserProps(user);}).then(function(userProps){userProperties=userProps;returnsomeService.getUserFriends(user);}).t

javascript - Bluebird.JS Promise : new Promise(function (resolve, reject){}) vs Promise.try(function(){})

我什么时候应该使用哪个?以下是一样的吗?新的Promise()示例:functionmultiRejectExample(){returnnewPromise(function(resolve,reject){if(statement){console.log('statement1');reject(thrownewError('error'));}if(statement){console.log('statement2');reject(thrownewError('error'));}});}Promise.try()示例:functiontryExample(){return

javascript - ES6 类中的 Promises

这个问题在这里已经有了答案:JavaScript"this"referenceswrongobject[duplicate](3个答案)关闭6年前。我正在尝试编写一个具有返回promise和promise链的方法的类。此尝试从do_that()返回错误我理解使用“this”的问题,这就是为什么我使用self=thiskludge,但我仍然遇到错误。TypeError:Cannotreadproperty'name'ofundefined.除了这个问题,我该如何解决这个问题,有没有更简洁的方法来做到这一点?varPromise=require('bluebird');classmyCla

javascript - 如果我拒绝具有另一个 Promise 值的 Promise 会怎样?

如果Promisep使用Promise(或Thenable)q的值解析,它本质上成为Promiseq的副本。如果q被解析,p将被解析为相同的值。Promise.resolve(Promise.resolve("hello"));Promise{[[PromiseStatus]]:"resolved",[[PromiseValue]]:"hello"}如果q被拒绝,p将被拒绝并具有相同的值。Promise.resolve(Promise.reject(newError("goodbye")));Promise{[[PromiseStatus]]:"rejected",[[PromiseV

javascript - 为什么 $state.go 在目标状态或其父级通过 promise 解析时不起作用

我尝试使用resolve在父状态上加载一些数据,并在应用程序运行时将用户重定向到默认状态:app.config(['$stateProvider','$urlRouterProvider',function($stateProvider,$urlRouterProvider){$stateProvider.state('home',{url:'/',template:'StartAppHomeOtherstateLoading...',resolve:{user:['$timeout','$q',function($timeout,$q){vardeferred=$q.defer();

javascript - 使用 Promises 时卡在变量上的最佳实践

这个问题在这里已经有了答案:HowdoIaccesspreviouspromiseresultsina.then()chain?(17个答案)关闭7年前。我是Promises的新手,我想知道在链中向下移动时保持变量的最佳实践是什么?通过Promise连接到MongoDB非常简单:connectToMongoDB(data).done(function(db){varcollection=db.collection('test_inserts');//domorestuffhere});但是如果我必须连接到两个不同的数据库会怎样呢?connectToMongoDB1(data1).the

javascript - 获得第一个实现的 promise

如果我有两个promiseA和B,其中只有一个会成功,我如何才能获得成功实现的那个?我正在寻找类似于Promise.race的东西,但它只会返回第一个实现的promise。我正在使用来自ES6的promise。 最佳答案 反转promise的极性,然后你可以使用Promise.all,因为它拒绝第一个被拒绝的promise,反转后对应于第一个实现的promise:constinvert=p=>newPromise((res,rej)=>p.then(rej,res));constfirstOf=ps=>invert(Promise.

javascript - jQuery Promise 然后在 AJAX 之后不工作

我的Promise是这样定义的:myFunc=function(){$.getJSON("./rest/api/some/url",function(json,textStatus){console.log("AJAXcallhit!");});};$.when(myFunc()).then(function(){console.log("Thenblockhit!");});在控制台中输出为:Thenblockhit!AJAXcallhit!我需要AJAX调用hit!,然后是Thenblockhit!。知道为什么会这样吗?我什至尝试实现自定义回调函数(我在Stackoverflow上

javascript - '错误 : Uncaught (in promise): No provider for Jsonp' (HTML/Javascript/Typescript/Angular2)

错误截图:.ts文件代码(SearchDisplay.component.ts):import{Component,OnInit}from'angular2/core';import{Router}from'angular2/router';import{Hero}from'./hero';import{HeroService}from'./hero.service';import{RouteConfig,ROUTER_DIRECTIVES}from'angular2/router';import{HeroesComponent}from'./heroes.component';imp