我正在使用Bluebird库做一些QA(问题/答案)应用程序。所以这是场景:用户在表格中填写一些问题的答案(例如5个问题)。一个问题有不止1个可能的答案:“问题有很多答案”答案在数据库中使用node.bcrypt加密(bcrypt)在遍历答案时,如果用户答案匹配,则无需继续检查该问题的答案。所以这是同步做事时要解决的一个常见问题,但我有点迷失了用promise做异步。这是我不知道如何进行的示例:.then(function(answers){varcompare=Promise.promisify(bcrypt.compare);//foreachanswer,Ineedtocheck
我有一个服务,PageService,我这样测试(简化)...vardatabase=require("../database/database");varPageService=require("./pageService");describe("PageService",function(){varpageService={};before(function(done){pageService=newPageService(database);}it("cangetallPages",function(done){pageService.getAll(function(err,pa
Promise现在是es6中的全局保留字,并且linters会抛出错误。那么这样做有什么坑varPromise=require("bluebird");或者我应该做什么varBluebirdPromise=require("bluebird"); 最佳答案 只要不是全局的,重新声明promise似乎没有问题。但是第二种是更好的方法Manyofusdothat.There'snoproblem.You'rejustusingafasterimplementation,that'sall.Butnotethatyoumayusemore
我正在使用NodeJS,bcrypt-nodejs(https://github.com/shaneGirish/bcrypt-nodejs)和Bluebird用于promises。想出了这段代码,并想知道是否有更好的方法来做同样的事情。我有模块:varPromise=require("bluebird"),bcrypt=Promise.promisifyAll(require('bcrypt-nodejs'));//....[someothecodehere]Users.prototype.setPassword=function(user){returnbcrypt.genSalt
我正在迁移现有程序以使用async/await(通过Babel的bluebirdCoroutines)为了学习这种风格。我一直在看这个tutorial.我对以下行为有点困扰。此代码段按预期工作:letparts=[];leturlsP=urls.map((url,index)=>{returndlPart(url,index,tempDir);});for(leturlPofurlsP){//Parallel(yay!)parts.push(awaiturlP);}for(letpartofparts){//SequentialawaitappendFile(leFile,part);
我有一个promise链,中间有一个递归promisedoAsyncRecursive(),如下所示:doAsync().then(function(){returndoAsyncRecursive();}).then(function(){returndoSomethingElseAsync();}).then(function(result){console.log(result);}).catch(errorHandler);doAsyncRecursive()必须做一些事情,如果一开始没有成功,我之后想每5秒尝试一次,直到它成功。这是我的promise函数的样子:functio
看起来非常基本,基于Promise的实现没有返回我期望看到的值/数据。这就是我期望此界面的工作方式:sdk.request(options)=>Promise~Response→Object(JSON)这是我模型中的代码:returnsdk.request(options).then(function(value){returnvalue;});当我记录模型的返回时,我看到了这个:{_bitField:0,_fulfillmentHandler0:undefined,_rejectionHandler0:undefined,_progressHandler0:undefined,_pr
这个问题在这里已经有了答案:HowtochainandsharepriorresultswithPromises[duplicate](1个回答)关闭5年前。有更好的方法吗?letfoo;returnfunctionA().then(result=>{foo=result;returnfunctionB();}).then(bar=>{returnfunctionC(foo,bar);});请注意,functionA的结果需要输入到functionC。在promise范围之外使用变量工作正常,但感觉有点恶心。有没有一种干净的惯用方法可以做到这一点?请注意,我没有机会更改我正在调用的任何
我不一定想出错,但我有:getFromDb().then(tradeData)->ifnottradeData#DONOTCONTINUETHECHAINelsegetLatestPricetradeData.then(latestPrice)->....then->....then->....catch(err)->nexterr如果没有tradeData,我有什么方法可以中止链条? 最佳答案 虽然是一个公认的答案,但我想告诉所有的谷歌人,“break()”函数已更改为“cancel()”像这样使用:p=getFromDb().t
我希望使用bluebird循环执行一些任务,只是将超时用作实验机制。[不打算使用异步或任何其他库]varPromise=require('bluebird');varfileA={1:'one',2:'two',3:'three',4:'four',5:'five'};functioncalculate(key){returnnewPromise(function(resolve,reject){setTimeout(function(){resolve(fileA[key]);},500);});}Promise.map(Object.keys(fileA),function(key