q

Node.js and asynchronous programming with Q

做~自己de王妃 提交于 2019-12-02 17:47:20
问题 Is the following the correct way to go about things? This is a sign up controller action. Im creating a user and a group to add the user to. Notice I have method level variables called user and group. The rest of the code is asynchronous using the Q module. Is it ok to have the method level variables or will they be overriden by the another person signing up at the same time? exports.postSignUp = function(req, res, next) { var user, group; return Q.invoke(exports, 'parseUser', req, null)

How to properly abort a node.js promise chain using Q?

。_饼干妹妹 提交于 2019-12-02 17:27:26
I'm using the Q module for Node.js in attempts to avoid the "pyramid of doom" in scenarios where I have many steps. For example: function doTask(task, callback) { Q.ncall(task.step1, task) .then(function(result1){ return Q.ncall(task.step2, task); }) .then(function(result2){ return Q.ncall(task.step3, task); }) .fail(callback).end(); } Essentially this seems to work; if an error is thrown by any of the task steps, it is passed to the callback (though I would be welcome to improvements, as I am new to node.js promises). However, I have a problem when I need to abort the task-chain early. For

Multiple chained deferred functions using q in AngularJS stop returning data

北城余情 提交于 2019-12-02 15:48:50
I am trying to chain together multiple deferred function calls such that the next call gets the results of the previous deferred.resolve. When I chain together more than 2 of these calls, the data stops being returned. Here is the basic code inside an angular controller: $scope.runAsync = function() { var asyncFn1 = function(data){ var deferred = $q.defer(); $timeout(function(){ console.log("Async fn1 " + data); $scope.outputLines.push("Async fn1 " + data); deferred.resolve("Async fn1 " + data); },1000); return deferred.promise; } var asyncFn2 = function(data){ var deferred = $q.defer();

How does Angular $q.when work?

一曲冷凌霜 提交于 2019-12-02 15:41:38
Can some one explain me how does $q.when work in AngularJS? I'm trying to analyse how $http work and found this: var promise = $q.when(config); And here is config object from Chrome console: Object {transformRequest: Array[1], transformResponse: Array[1], cache: Object, method: "GET", url: "/schedule/month_index.html"…} cache: Object headers: Object method: "GET" transformRequest: Array[1] transformResponse: Array[1] url: "/schedule/month_index.html" __proto__: Object What happens next? How this object get's resolved or rejected? Calling $q.when takes a promise or any other type, if it is not

sailsjs use mongodb without ORM

拥有回忆 提交于 2019-12-02 12:29:33
I want to use mongodb with sails but without any ORM. So below is my service to connect mongodb. Service: //DbService.js const MongoClient = require('mongodb').MongoClient; module.exports = { db:function(req, res){ var connect=MongoClient.connect("mongodb:***********").then(function (err, database) { if(err) console.log(err); else{ database=database.db('*****'); return connect; } }); } } After connection i have called it in controller, But getting TypeError: Cannot read property 'then' of undefined. controller: //HomeControlelr.js module.exports = { index:function(req, res){ DbService.db()

Function Returns a Promise, Check Errors

我们两清 提交于 2019-12-02 11:16:15
问题 I have a function doSomething() that returns a promise chain, utilizing the Q framework. The contents are similar to something like: loadDataSet : function (params) { return Q.fcall(function() { //Do Something }) .then(function(){ //Do Something Else throw New Error('unexpected error'); }); } The calling code goes something like: var promise = loadDataSet(args); I want to figure out whether that error was thrown. Notice, in the loadDataSet function implementation, I did not utilize the .done(

Q promises - Node.js function for every element in the array

筅森魡賤 提交于 2019-12-02 10:39:27
问题 Function dirList() should return array of folders inside definded directory. I can't understand how return dirList variable only after the function isDir() is executed for each file. I guess that I should use Q.all() , but I don't know where I should put it :-( var fs = require('fs'), Q = require('q'), readdir = Q.denodeify(fs.readdir); function isDir(path) { return Q.nfcall(fs.stat, __dirname + path) .then(function (stats) { if (stats.isDirectory()) { return true; } else { return false; } })

Node.js and asynchronous programming with Q

久未见 提交于 2019-12-02 10:09:11
Is the following the correct way to go about things? This is a sign up controller action. Im creating a user and a group to add the user to. Notice I have method level variables called user and group. The rest of the code is asynchronous using the Q module. Is it ok to have the method level variables or will they be overriden by the another person signing up at the same time? exports.postSignUp = function(req, res, next) { var user, group; return Q.invoke(exports, 'parseUser', req, null) .then(function(u) { user = u; return Q.invoke(exports, 'postCreateUser', user).fail(function(err) { throw

Restart a promise after fail

邮差的信 提交于 2019-12-02 08:32:26
I'm using Nodejs and Q to run a sequence of asynchronous functions. If one fails i'd like to run another function and then start the sequence again. Heres it is as is: var promise = database.getUserCookies(user) .then(function (data){ return proxy.search(data); }) .fail(function (data) { if (data.rejected === 302) { var relogin = database.fetchAuth(data) .then(function (data) { return proxy.login(data) }) .then(function (data){ return database.saveCookies(data); }) .then(function (data){ //Restart the promise from the top. }) } }) .then(function (data){ return responser.search(data); }) You

Q promises - Node.js function for every element in the array

自古美人都是妖i 提交于 2019-12-02 05:05:38
Function dirList() should return array of folders inside definded directory. I can't understand how return dirList variable only after the function isDir() is executed for each file. I guess that I should use Q.all() , but I don't know where I should put it :-( var fs = require('fs'), Q = require('q'), readdir = Q.denodeify(fs.readdir); function isDir(path) { return Q.nfcall(fs.stat, __dirname + path) .then(function (stats) { if (stats.isDirectory()) { return true; } else { return false; } }); } function dirList(path) { return readdir(__dirname + path).then(function (files) { var dirList =