Tag: 承诺

将BlueBird同步链接在一个数组中

我试图让一系列的同步执行的承诺,链接在一起,但只有基于条件添加某些承诺。 下面是我的意思的例子: const Promise = require('bluebird') const funcA = int => new Promise( res => res(++int) ) const funcB = int => new Promise( res => res(++int) ) const funcC = int => new Promise( res => res(++int) ) let mainPromise = funcA(1) // Only execute the funcB promise if a condition is true if( […]

如何限制/控制太多的asynchronous调用?

我对Node很新,但是我明白编写syncronous函数是件坏事。 我locking了事件循环或者其他东西…所以把所有的东西都写成asyncronous是很好的。 但是在某些情况下,写一切asynchronous可能是不好的。 举例来说,我有一个API调用函数(对第三方API服务),然后我必须将结果写入数据库。 我需要在短时间内完成这个工作,例如500次。 调用这个API 500次asynchronous,然后写入数据库500次asynchronous可能会禁止我的API服务(节stream),并超载我的数据库服务器。 什么是控制或限制这样的事情的最好方法? 我想保持asynchronous,所以它是有效的,但我不能采取上述方法。 我研究了一些Promise节stream方法。 这是解决这类问题的正确方法吗? 有没有更好的方法来做到这一点?

使用request-promise嵌套asynchronous请求

我正在使用Visual Studio Online API并试图通过存储库获取分支统计信息。 要做到这一点,我嵌套我的asynchronous调用。 我正在使用请求承诺来解决我的GET请求。 我遇到的问题是在将所有分支添加到顶级模型后,如何返回模型: 当我console.log结果我明显得到[]因为它没有解决分支请求。 var model = []; rp(options) .then(function(repos) { repos.value.forEach(function(repository) { var repo = { id: repository.id, name: repository.name, branches: [] }; var branchOptions = options; branchOptions.url = config.endPoints.base + config.endPoints.branches(repository.id); rp(branchOptions) .then(function(branches) { branches.value.forEach(function(branch) { repo.branches.push({ name: branch.name, behind: branch.behindCount, ahead: branch.aheadCount }); }) }).then(function() { model.push(repo); }); […]

在返回承诺后突破function – JavaScript

当你有一个基于Promise的函数时,像这样: function foo() { return new Promise(function(fulfill, reject) { // Do some async work, and then… console.log('a'); fulfill('b'); console.log('c'); }); } 你会注意到c将会在fulfill声明后被打印,意味着函数在履行或拒绝时不会中断。 这很麻烦,因为大多数逻辑会假定函数一旦调用了fulfill()或reject() 。 题: 在fulfill()或reject()调用之前,仅仅添加return是安全的还是标准的用法? function foo() { return new Promise(function(fulfill, reject) { // Do some async work, and then… console.log('a'); return fulfill('b'); console.log('c'); //Does not get printed, as the function has ended execution. […]

Node.js Web服务器fs.createReadStream vs fs.readFile?

所以我正在写我的web服务器在纯node.js,只使用bluebird promisify 。 这一直困扰我一个星期,我不能决定我应该使用哪一个。 我已经阅读了大量关于这两个主题的post,博客和文档,请根据您自己的工作经验回答,谢谢。 这里有详细的总结和相关的问题。 两种方法都经过testing,它们都很好。 但是我不能testing性能,我只有自己的基本网站文件(html,css,img,小型数据库等),而且我从来没有pipe理过的video文件和庞大的数据库。 下面是代码部分,给你一些基本的想法(如果你真的知道使用哪一个,不用费心去阅读代码,为了节省一些时间),这个问题不是关于逻辑,所以你可以阅读虚线。 关于fs.createReadStream : 优点:对于大文件很好,它一次读取一个块,节省内存和pipe道真的很聪明。 缺点:同步,不能被promisified(stream是一个不同的概念承诺,太难做,不值得)。 //please ignore IP, its just a custom name for prototyping. IP.read = function (fpath) { //—————————————————- let file = fs.createReadStream(fpath); file.on('error', function () { return console.log('error on reading: ' + fpath); }); return file; //—————————————————- }; //to set the response of onRequest(request, […]

Node.js拦截Promise并详细说明Response

我开始使用Node.js + Express,现在正在编写HTTP API结构。 我有一个控制器,使用以下模式公开一些方法: my_controller.js 'use strict'; var AppApiFactory = function (express, appService) { var api = express.app; /* Get all apps ordered by Name Ascending */ function getAllApps(request, response) { appService.getApps(request.query.$expand).then(function (apps) { response.status(200).send(apps); }) .catch(function (err) { console.error('Error occurred in Apps Api: ' + err); response.status(500).send("" + err); }); } /* Routing […]

用摩卡expressiontesting:基于承诺的testing不会自行运行?

我有两个testing文件。 当只有test1.js存在时,没有testing运行,并且mocha报告“0通过”testing。 当test1.js和test2.js存在,但都依赖于承诺,那么仍然没有testing运行,摩卡仍然报告“0通过”。 但是,当其中一个testing被修改为不使用诺言时,摩卡运行两个testing,他们成功。 有没有搞错? 这是我的文件: index.js: require('./server').then( function(server) { server.listen(8080, function() { console.log("Started server"); }); ); server.js: var express = require('express'); var server = express(); module.exports = new Promise((function(resolve, reject) { return resolve(server); })); test1.spec.js: require('./server').then(function(server) { describe('Test Suite #1', function () { it('should run test #1', function testSomething(done) { return done(); }); }); […]

返回嵌套的错误,并承诺ExpressJS / NodeJs

我从函数调用返回错误有点混淆。 例如,我使用sequelizeJS来说明这一点。 一般: First.Ctrl var second_ctrl = require( '../ctrl/second'); testCode : function(req, res, next){ return second_ctrl.getData() .then(function(resultData){ res.json(resultData); }) .catch(function(error){ res.json(error) }) } Second.Ctrl getData : function(){ return models.Data.findAll() } getData findAll中的任何错误都会转到first_ctrl的catch块。 但是,如果我必须做一些操作,如: Second.Ctrl – 操作 getData : function(){ return models.Data.findAll() .then(function(result){ if(result == null) throw new Error ('No data found'); return result; }) .catch(function(error){ […]

Node.js – 使用Promise.all()加载和执行多个函数

我正在开发一个“插件”的概念,我有一系列的文件,每个文件包含一个单一的function(插件)。 我想使用promise.all()自动加载和执行这些。 问题:每个插件函数都不执行。 这里是我的例子plugin plugins/example.js : "use strict"; exports = function() { return new Promise(function(resolve, reject) { console.log("Plugin running…."); setTimeout(resolve, 200, 'example plugin succeeded!'); }); }; 从我的app.js中,然后使用require-all NPM模块加载所有插件: const plugins = require('require-all')(__dirname + '/plugins'); 然后我尝试执行所有的作为我的承诺链的一部分: return Promise.all([plugins]); 没有logging从function发生。 有趣的是,当我loginplugins的内容,我看到和空对象: { "example": {} } 任何人都可以build议为什么示例函数没有被调用?

使用await时如何得到err和结果

我有一个函数接收callback作为参数。 例如: client.sendMessage(params, (status, response) => { console.log('Status: ', status); console.log('API Response:\n', response); }); 我然后promisify它: const Promise = require('bluebird'); const sendMessageFunc: Object = Promise.promisify(client.sendMessage); 然后我称之为: result = await sendMessageFunc(params); 我其实想要(status, response)然后做: (status, response) = await sendMessageFunc(params); console.log('Status: ', status); console.log('API Response:\n', response); 但是这不是有效的语法。 我该怎么办? 什么是返回给我的“结果”对象?