Tag: promise

JavaScript承诺重build意想不到的令牌错误

您好,我试图使用nodeJS承诺的请求模块。 这是我的脚本 require('promise'); var request = require('request'); const requestget = function (url) { return new Promise((resolve, reject) { request(url, function (error, response, body) { if (!error && response.statusCode == 200) { resolve(body); } else { reject(error); } }); }); } requestget('http://www.modulus.io').then(console.log); 但是得到以下错误。 /home/xyz/reques.js:14 return new Promise((resolve, reject) { ^ SyntaxError: Unexpected token { at Module._compile […]

范围内的变化承诺“那么”方法

我写了下面的程序,每N毫秒处理一个目录中的所有文件: fs = require "fs" Q = require "q" class FileProcessor constructor: (options) -> @dir = options.dir @delay = options.delay processFiles: -> Q.nfcall fs.readdir, @dir .then (files) -> Q.all files.map (file) -> @processFile file # <– Error! .then -> Q.delay @delay .then -> @processFiles() .done() processFile: (file) -> deferred = Q.defer() # Does some stuff. […]

如何在node.js中使用q.defer来链接promise?

exports.list = function(req, res) { var location_parent_id = req.params.location_parent_id; var query = { company_id: req.company_id }; if(location_parent_id) { query.location_parent_id = location_parent_id; Location.findOne({someQuery}, function(err, location) { response.location = location; }); } else { query.location_parent_id = { '$exists': false } } Location.find(query, function(err, locations) { if(err) { response = { status: 'error', error: err } } else […]

链接返回承诺数组的函数

我有类似于以下内容的东西,想知道是否有一种“链式”的方式来做到这一点,或者如果我不符合标准,这代表了一种气味。 谢谢! var promises = Q.all(returns_a_promise()).then(returns_array_of_promises); var more_promises = Q.all(promises).then(returns_another_array_of_promises); var even_more_promises = Q.all(more_promises).then(yet_another_array_o_promises); Q.all(even_more_promises).then(function () { logger.info("yea we done"); }); 理想情况下是这样的: Q.all(returns_a_promise()) .then(returns_array_of_promises) .all(returns_another_array_of_promises) .all(yet_another_array_o_promises) .all(function () { logger.info("yea we done"); });

JS承诺 – 使这个承诺嵌套更有效的方法?

考虑这段代码: WordPress.getMediaById(res.data.featured_media) .then(function (res) { post.featuredMedia = res.data.source_url; WordPress.getUserById(post.authorId) .then(function (res) { post.authorName = res.data.name; $scope.post = { title: post.title, introAsHtml: post.introAsHtml, authorName: post.authorName, contentAsHtml: post.contentAsHtml, featured_media: post.featuredMedia }; }); }); 有什么办法可以使它在嵌套方面更有效率? 在将来,我会想添加更多的承诺function,我不知道是否是正确的方式,否则,它是如何真正不同于callback… 问候。

如何使用Q库,同时转换节点asynchronousfunction承诺?

如何在下面的代码中使用Promise? function async_f1(callback) { setTimeout(function() { callback("Async function 1…"); }, 1000); } function async_f2(callback) { setTimeout(function() { callback("Async function 2!…"); }, 1000); } function async_f3(callback) { setTimeout(function() { callback("Second async function 3!…"); }, 1000); } function doAll() { async_f1(function(result1) { async_f2(function(result2) { async_f3(function(result3) { console.log("Final result:", result1 + " " + result2 + " " […]

从Q Promise开始顺序运行

我有这个代码,我希望这两个Promise按顺序运行,结果应该是这样的: a progress: 20 a progress: 40 a progress: 60 a progress: 80 a progress: 100 a resolve: a b progress: 20 b progress: 40 b progress: 60 b progress: 80 b progress: 100 b resolve: b 但我得到这个: a progress: 20 b progress: undefined a progress: 40 b progress: undefined a progress: 60 b progress: […]

Q不能正确链接

我尝试链接承诺。 下面的getExternalMap()返回一个promise(处理ajax call => deferred.resolve()并返回deferred.promise)。 “第一链”被称为没有问题,但“二链”不叫,为什么? 我认为这是写得很好,但不是,我也找不到为什么。 var self = this; getExternalMap().then(function() { console.log("First chaining"); var parsedMap = self.destinationsApiParser.getSortDestinations(); return self.initProxies(parsedMap); }).then(function() { console.log('second chaining'); }); this.initProxies = function(parsedMap) { var deferred = Q.defer(); var destinationsApiProxy = new DestinationsApiProxy(this.app, parsedMap); deferred.resolve(); return deferred.promise; }

illegal_argument_exception请求包含无法识别的参数: – ElasticSearch索引创build

自从昨天以来,我第一次使用elasticsearch,并且由于我的知识有限,在两天的工作之后,我正在努力获得简单的function。 我的主要目标是用NodeJs + ElasticSearch完成一个糟糕的工作。 现在我坚持通过使用“映射”function创build索引。 我直接的问题是:为了使这个代码有效,我必须做些什么? return client.indices.create({ index: 'index_created_with_map', mapping: { posts: { user: { type: 'string' }, post_date: { type: 'string' }, message: { type: 'string' } } } }); 任何build议什么检查将不胜感激。 另外,虽然不是我主要问题的一部分,但是如何将get和search函数正确地返回给“response.send(JSON.stringify(来自elasticsearch的数据)”)以及如何将“post_date”映射到datetypesstring将不胜感激,因为我卡在它。 贝娄是我迄今为止所尝试过的。 我可以看到抛出“Chrome ElastiSearch工具箱扩展”,当我尝试没有“映射”function像addToIndex下面它的工作,但我想有单独的function,一个用于创build索引,我将只运行一次明显和另一个创build“logging”将成为我的一部分。 PS。 我在这里发现了非常类似的问题,没有任何答案 illegal_argument_exception:没有为字段find映射 错误日志: Unhandled rejection Error: [illegal_argument_exception] request [/index_created_with_map] contains unrecognized parameter: [mapping] at respond (/home/demetrio/dev/WSs/NodeJs/greencard-dmz-es-oracle/node_modules/elasticsearch/src/lib/transport.js:289:15) at […]

SocketCluster中间件握手与承诺

我build立一个既服务于http和ws的应用程序。 用户首先通过HTTPlogin到Laravel服务器。 这将返回一个用于允许通过WSlogin的JWT。 Ihv添加了一个MIDDLEWARE_HANDSHAKE,它获取该令牌并向Laravel Server发出请求,询问该令牌是否有效,并且该用户有权访问WS(并非每个已login的用户都可以访问WS); 客户代码: var options = { host: '127.0.0.1:3000', query: { source: 'web', token: '', } }; var socket; $.post('http://127.0.0.1:8000/authenticate', { email: 'chadd01@example.org', password: '1234' }, function(data, textStatus, xhr) { options.query.token = data.token; //ALL PERFECT UNTILL HERE // Initiate the connection to the ws server socket = socketCluster.connect(options) .on('connect', function(data) { console.log('CONNECTED', […]