bluebird

Bluebird mapSeries

一曲冷凌霜 提交于 2019-12-09 16:41:21
问题 I am trying to execute a series of promises in order, only going to the next one after the previous is resolved. From Bluebird docs: The iterator won't be called for an item until its previous item, and the promise returned by the iterator for that item are fulfilled. http://bluebirdjs.com/docs/api/promise.mapseries.html var Promise = require('bluebird'); function test(time) { return new Promise(function(resolve, reject) { setTimeout(function() { console.log(time); resolve(time); }, time); })

初识JavaScript Promises之二

断了今生、忘了曾经 提交于 2019-12-09 13:36:41
初始JavaScript Promises之二 上一篇我们初步学习了JavaScript Promises,本篇将介绍Promise如何优雅地进行错误处理以及提升操作 node.js风格 1 的异步方法的逼格,没错就是使用 promisify 2 。 异步编程中的错误处理 人性的、理想的也正如很多编程语言中已经实现的错误处理方式应该是这样: try { var val = JSON.parse(fs.readFileSync("file.json")); }catch(SyntaxError e) {//json语法错误 console.error("不符合json格式"); }catch(Error e) {//其它类型错误 console.error("无法读取文件") } 很遗憾,JavaScript并不支持上述方式,于是“聪明的猴子”很可能写出下面的代码: try { //code }catch(e) { if( e instanceof SyntaxError) { //handle }else { //handle } } 相信没人会喜欢第二段代码,不过传统的JavaScript也只能帮你到这里了。 上面的代码是同步模式,异步模式中的错误处理又是如何呢? fs.readFile('file.json', 'utf8', function(err, data){ if

Testing Restify Route Handler that contains Promise Code Block, using SinonJs and Mocha

安稳与你 提交于 2019-12-08 21:23:26
I have a restify action code block below: function retriveAll(req, res, next) { db.user .find({where: {id: 1}) .then(function(user){ res.send(user); }) .catch(function(details){ res.send(details.message); }) .finally(function(){ next(); }); } I want to test this action specifically validating that res.send() was called within this code block . And later on validating the res.send() returned data. I'm using SinonJs and Mocha for testing framework. Here's a sample test code block for the method above. describe('retrieveAll()', function() { reqStub = {}; resStub = {send: sinon.stub()}; nextStub =

How to handle asynchronous error in Node.js

☆樱花仙子☆ 提交于 2019-12-08 07:33:06
问题 Is there any alternative to Bluebird's Promise.try function. As I'm using async/await not interested in adding bluebird dependency. Is there a better way to capture asynchronous error in Node.JS await Promise.try(async function() { // Some code with async operations like readline await this.nextCall(batchData, true); }).catch(async function(err) { // My error handling }); Any inbuilt functions in Node.js 10.x ? UPDATE : Is there a better way to capture asynchronous error in Node.JS try { let

How to load bluebird (pulled from NPM) into a Dojo project as an AMD module?

老子叫甜甜 提交于 2019-12-08 06:49:42
问题 I am working on a Dojo project which uses a number of NPM packages, one of them being bluebird as I need to use Promise in IE. I am looking for the best practice/recommended way to load NPM packages into my project. The following code is an example illustrating my question: require([ 'dojo/dom', 'dojo/request', 'dojo/domReady!' ], function(dom, request) { var message = dom.byId('greeting'); message.innerHTML = "Getting started"; var p1 = new Promise(function(resolve, reject) { setTimeout

Nodejs exec mongodb command in Bluebird Promise

给你一囗甜甜゛ 提交于 2019-12-08 00:49:31
问题 I want to run mongod command in node.js using Promises, so that database operations can run only after mongodb process is started. I tried my hands with following, but failed: var Promise = require("bluebird"); var execAsync = Promise.promisify(require('child_process').exec); execAsync("~/mongodb/bin/mongod").then(function(result){ console.log("started mongodb..."); }).catch(function(error){ console.log("error in starting mongodb..."+JSON.stringify(error)); }); Any suggestions? 回答1: You

Bluebird — a promise was created in a handler but was not returned from it

橙三吉。 提交于 2019-12-08 00:13:58
问题 First of all, I know that I have to return promises to avoid this warning. I've also tried returning null as suggested here in the docs. Consider this piece of code, I'm using it in Mongoose's pre-save hook, but I've experienced this warning in other places: var Story = mongoose.model('Story', StorySchema); StorySchema.pre('save', function(next) { var story = this; // Fetch all stories before save to automatically assign // some variable, avoiding conflict with other stories return Story.find

Express.js and Bluebird - Handling the promise chain

不羁的心 提交于 2019-12-07 17:35:10
问题 In a backend API I have a login route which should perform the following sequence of actions: Given an username and password, try to authenticate the user against an Active Directory. If authentication has failed reply with status 401. If success, continue. Look for an user with the given username in the database. If not found reply with status 403, otherwise continue. Find if the user document has some details like email, display name, etc (in case this is not the first time logging in). If

Chaining Requests using BlueBird/ Request-Promise

◇◆丶佛笑我妖孽 提交于 2019-12-07 16:12:30
I'm using the request-promise module and have found no mention of how to chain requests. I'm currently following their syntax of: request({options}) .then(function(result){...}) .catch(function(error){...}) However I want to be able to use Promise.all and try to make multiple calls at the same time and wait for them to all resolve and then proceed with other calls. For example I want to: Make a call to one app creating a User. AT THE SAME TIME, make a call creating an Address. Promise.all([UserCall, AddressCall]).then({function to deal with results])? Also I have been working with my function

Nested For Loops converted to Nested Promises

倾然丶 夕夏残阳落幕 提交于 2019-12-07 14:32:22
问题 I'm running into a problem where my program ends only on one iteration of the nameList , and I'm not exactly sure where the illogical code lies. Given globally: var _ = require("underscore"); var nameList = ["Bob", "Susie"] var jsonDict = {} My complicated nesting starts here, but I'm not sure how to fix it so that it iterates through both the nameList and the numbers for-loop 1-10 : return new Promise((res, rej) => { var promises = []; return Promise.map(nameList, function(personName){ for