deferred

Make function wait for $http response in AngularJS

*爱你&永不变心* 提交于 2019-12-13 00:27:45
问题 I have a method seatClicked() that calls getUserID() to get the user id corresponding to the session atribute 'user'. There is a table that contains the username and user id(unique). The following is the definition of seatClicked() $scope.seatClicked = function() { promise = $scope.getUserID(); promise.then(function(results){ $scope.seatID.userID = results; // store the user id in $scope.seatID.userID }); } This is the definition for getUserID() $scope.getUserID = function() { var deferred =

Deferred in a while loop

六月ゝ 毕业季﹏ 提交于 2019-12-12 18:17:14
问题 So I want to do a deferred ajax request in jquery until I receive a specific server response (non-null). How would I go about doing that? while (data.response != null) { $.ajax(..).done(function(data); } function doUntilResult() { Server.doA().done(function(data) { Server.doB(data).done(function(result) { //if result == null, repeat again }); }); } 回答1: To give a large measure of control, write doUntilResult() to accept : a doThis callback function, which determines what is to be done, and

What's the correct pattern for Promise.defer?

試著忘記壹切 提交于 2019-12-12 16:21:08
问题 I'm using TypeScript and async / await to represent an asynchronous workflow. Part of that workflow is to call out to a web worker and continue when it calls back with a result. In C#, I'd create a TaskCompletionSource , await its Task and elsewhere in the code would call SetResult to resolve the TaskCompletionSource . I can do the same thing in JavaScript by initializing a Deferrer object using Promise.defer() , await ing its Promise and elsewhere, in the window.onmessage listener would call

What's the meaning of $.when.apply(null, a method) in jQuery?

不打扰是莪最后的温柔 提交于 2019-12-12 10:53:48
问题 I'm reading the deferred object in jQuery. Could anyone please tell me what's the difference between following two invoking way? $.when.apply(null, a method).done(function(){success callback}) $.when.(a method).done(function(){success callback}) And what kind of cases are fit for the first way above? Thanks in advance. 回答1: $.when.apply(null, a method) only makes sense if a method is actually an array or a method call returning an array. Then it's like a $.when(elements, of, the, array) . See

mongoose and q promises

谁都会走 提交于 2019-12-12 10:47:47
问题 I'm working from the mongoose/q promises framework sample here, but seem to have some issues with the nfbind when trying to use findOne, mainly since the samples from the Q framework don't seem to match those in the gist. My code: var mongoose = require('mongoose'); var Q = require('q'); var user_schema = mongoose.Schema({username:String, last_touched:Date, app_ids:[String]}); var user = mongoose.model('user', user_schema); exports.user = user; exports.user.find = Q.nfbind(user.find); exports

jQuery .when().done() not working

你。 提交于 2019-12-12 08:21:38
问题 I'd like to start by saying I'm new to jQuery and I suspect I'm just doing something stupid, so hopefully this will be very simple for someone. I'm trying to add a sliding mobile sub-menu to my website. I want an accordian effect whereby if I click one parent link, it's child sub-menu opens and all other sub-menus close. The problem is timing - the child sub-menu opens and then is closed again by the resetting of all sub-menus. I presume the answer is to use deferreds but everything I've

Problems with deferred object

↘锁芯ラ 提交于 2019-12-12 06:35:48
问题 I have a problem i´m trying to solve by using deferred objects in javascript (and i am new to deferred objects). The problem: The user tries to run a function (can be alot of different functions). If the function fails...it will try to login again and then try again (one time). If login fails. Well then all fails. These functions and the login function contains an Ajax call that will be returned. My Question is: Can i rely on that var dfd (at the end of the tryAjax function) to be executed

How to get a deferred handler to return a value to the calling function?

别说谁变了你拦得住时间么 提交于 2019-12-12 05:42:36
问题 Look at the below example to understand what I am trying to do: //Caller.js callingFunction : function (...) { var a = new Assistant(); console.log("This object has been returned ", a.showDialog(...)); }, //Assistant.js showDialog : function (...) { deferred.then(lang.hitch(this, this._showDialog)); //I want to return someObject to callingFunction }, _showDialog : function (dialogData) { ... ... return someObject; },} 回答1: Since it's deferred, there is nothing for it to return before that

using jquery's deferred with settimeout

别说谁变了你拦得住时间么 提交于 2019-12-12 03:56:53
问题 I have a module that has an internal loop (using 'settimeout'). I want to fire a callback each time the timer happens. I tried using jQuery's deferred object with no luck.. Something like: $(function(){ var module = new MyModule(); $.when(module.init()).then("DO Something"); }); function MyModule(){ this.d = $.Deferred(); } test.prototype.init = function(){ clearTimeout(this.next); var self = this; /* Do Something */ self.d.resolve(); this.next = setTimeout(function(){ self.init(); /* The

$routeprovider, resolve with deferred variable

∥☆過路亽.° 提交于 2019-12-12 03:18:30
问题 I am trying to only load this view when dbReadyDeferred is resolved. dbReadyDeferred is defined somewhere else (JQuery is available) in common.js: var dbReadyDeferred = $.Deferred(); view1.js: 'use strict'; angular.module('myApp.view1', ['ngRoute']) .config(['$routeProvider', function($routeProvider) { $routeProvider.when('/view1', { templateUrl: 'view1/view1.html', controller: 'View1Ctrl', resolve: dbReadyDeferred.promise }); }]) .controller('View1Ctrl', ['$scope', function ($scope) { ...