q

Resolving breeze query/Q promise with $route resolve stops page

天涯浪子 提交于 2019-12-04 14:23:54
I'm having problems executing a breeze query with angular resolve before the view is rendered. I'm trying to get some data from the server before the view is rendered with breeze. I'm using $routeProvider.when('/countries', { templateUrl: 'App/partials/countries.html', controller: Ctrl, resolve: Ctrl.resolve }). controller and service snippets: function Ctrl($scope, Q, datacontext, countries) { //... } function getCountries(forceRefresh) { var query = entityQuery. from("countries"). orderBy("name"); return manager.executeQuery(query). then(getSucceeded); } function getSucceeded(data) { return

Using Q library in browser

守給你的承諾、 提交于 2019-12-04 13:46:15
问题 I need to use Q library (http://documentup.com/kriskowal/q/) in the browser. I would like to use RequireJS to load this library, but I don't have any idea how to do this. I know how to load my own module, but I can't do it with Q . It has some function: (function (definition) { //some another code here*** // RequireJS } else if (typeof define === "function" && define.amd) { define(definition); How can I load Q and then use it in another module? 回答1: You can simply load the Q library using a

Mongoose and promises: how to get an array of query results?

最后都变了- 提交于 2019-12-04 08:08:41
问题 Using mongoose to query results from the db and Q for promises, but finding it hard to wrap my head around just getting a list of users that's available. Currently I have some something like this: var checkForPerson = function( person ) { people = mongoose.model('Person', Person) return people.findOne({"_id": person }, function(err, doc) { if (err) console.log(err) if (doc !== null) { return doc } else { console.log('no results') } }) } var promises = someArrayOfIds.map(checkForPerson); //

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

拥有回忆 提交于 2019-12-04 07:49:05
问题 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

Restart a promise after fail

本小妞迷上赌 提交于 2019-12-04 06:20:10
问题 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){ /

sailsjs use mongodb without ORM

巧了我就是萌 提交于 2019-12-04 06:03:06
问题 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

How do I rewrite a series of conditional statements with Q promises in node.js?

空扰寡人 提交于 2019-12-04 04:30:04
问题 exports.create = function(req, res) { var company_id = req.company_id; var client = new Client(req.body); Company.findOne({_id: company_id}, function(err, company) { if(err) { response = { status: 'error', error: err } return res.json(response); } else if(!company) { response = { status: 'error', error: 'Invalid company_id' } return res.json(response); } else { client.save(function(err) { if(err) { response = { status: 'error', error: err } } else { response = { status: 'ok', client: client }

How do I do a callback chain with q?

蹲街弑〆低调 提交于 2019-12-04 03:20:05
I some problems understanding how to use "q" (https://github.com/kriskowal/q) a promises library for javascript: var delayOne = function() { setTimeout(function() { return 'hi'; }, 100); }; var delayTwo = function(preValue) { setTimeout(function() { return preValue + ' my name'; }, 200); }; var delayThree = function(preValue) { setTimeout(function() { return preValue + ' is bodo'; }, 300); }; var delayFour = function(preValue) { setTimeout(function() { console.log(preValue); }, 400); }; Q.fcall(delayOne).then(delayTwo).then(delayThree).then(delayFour).end(); this only returns undefined... The

chaining promises with q.js

喜你入骨 提交于 2019-12-04 01:32:04
I'm trying to understand how promise chaining works. I'm using q.js . Here's what I'm playing with. var Q = require("q"); // npm install q // the function Q(value) returns a fulfilled promise with the value... I think. Q(1).then(Q(2)).then(Q(3)).then(Q(4)).then(function(n) { console.log(n); }); Q(1).then(function(n) { return Q(2); }).then(function(n) { return Q(3); }).then(function(n) { return Q(4); }).then(function(n) { console.log("done: " + n); }); My question basically boils down to why does the first one log 1 while the latter one logs what I would expect and basically logs 1 through 4. I

Javascript: How to iterate on array using promises?

删除回忆录丶 提交于 2019-12-04 00:18:51
问题 LIVE DEMO Given the following function: function isGood(number) { var defer = $q.defer(); $timeout(function() { if (<some condition on number>) { defer.resolve(); } else { defer.reject(); } }, 100); return defer.promise; } and an array of numbers (e.g. [3, 9, 17, 26, 89] ), I would like to find the first "good" number. I would like to be able to do this: var arr = [3, 9, 17, 26, 89]; findGoodNumber(arr).then(function(goodNumber) { console.log('Good number found: ' + goodNumber); }, function()