草庐IT

promised-mongo

全部标签

javascript - 当 promise 被多次解决时会发生什么

如果多次拒绝/解决ES6promise,标准行为是什么?以下代码仅在GoogleChrome中被解析一次,这是所有浏览器的标准行为吗?newPromise(function(e){$('#button').click(function(){resolve();});});我看到一个promisepolyfill在尝试解决已经解决的promise时抛出异常。es6-promise的规范是否指定了这一点,或者不符合polyfill标准?更新抱歉,我刚刚意识到它不是polyfill,而只是Promise(非标准)的最小实现。 最佳答案 一

javascript - 我如何判断一个对象是否是 Angular $q promise?

我的项目中有一个预先存在的非AngularAPI库。它有一个返回jQuery.Deferredpromise的.request方法。我创建了一个简单的Angular服务,它包装了.request方法以将其结果转换为Angular$qpromise。它看起来像这样:varmodule=angular.module('example.api',[]);module.factory('api',function($q,$window){functionwrappedRequest(){varresult=$window.API.request.apply($window.API,argume

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.